RichTextBox选择突出显示

RichTextBox选择突出显示

本文介绍了RichTextBox选择突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户选择后是否有可能每个选中的字母都显示其原始颜色?而且不是默认情况下总是白色吗?



我想实现类似



而不是



注意:




  • 由于这个古老的有用的Visual Studio工具,我可以使用Spy ++看到写字板的RichTextBox类。


  • 如果您的操作系统中的 RICHEDIT50W 有任何问题,可以打开 Spy ++ 写字板,然后选择其中的 RichTextBox 并查看类名是什么。 / p>







  • 当我搜索将 RICHEDIT50W 类应用于控件时,我到达了这是@Elmue的好帖子,多亏了他。


Is there a possibility that after selection made by user, every selected letter displays it's original color? And not always white as it's by default?

I want to achieve something like that which you can see in wordpad.

instead of which you see in RichTextBox.

解决方案

You can use the latest version of RichTextBox that is RICHEDIT50W, to do so you should inherit from standard RichTextBox and override CreateParams and set the ClassName to RICHEDIT50W:

Code

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ExRichText : RichTextBox
{
    [DllImport("kernel32.dll", EntryPoint = "LoadLibraryW",
        CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern IntPtr LoadLibraryW(string s_File);
    public static IntPtr LoadLibrary(string s_File)
    {
        var module = LoadLibraryW(s_File);
        if (module != IntPtr.Zero)
            return module;
        var error = Marshal.GetLastWin32Error();
        throw new Win32Exception(error);
    }
    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            try
            {
                LoadLibrary("MsftEdit.dll"); // Available since XP SP1
                cp.ClassName = "RichEdit50W";
            }
            catch { /* Windows XP without any Service Pack.*/ }
            return cp;
        }
    }
}

Screenshot

Note:

  • I could see the class of RichTextBox of wordpad using Spy++ thanks to this ancient useful visual studio tool.

  • If you had any problem with RICHEDIT50W in your os, you can open Spy++ and WordPad and then select the RichTextBox of it and see what's the class name.

  • When I searched about applying the RICHEDIT50W class to my control, I reached to this great post of @Elmue, Thanks to him.

这篇关于RichTextBox选择突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:38