Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RichTextBox选择突出显示_C#_.net_Winforms_Selection_Richtextbox - Fatal编程技术网

C# RichTextBox选择突出显示

C# RichTextBox选择突出显示,c#,.net,winforms,selection,richtextbox,C#,.net,Winforms,Selection,Richtextbox,是否有可能在用户选择后,每个选定的字母都显示其原始颜色?而且不总是默认的白色 我想实现这样的目标 你可以在写字板上看到 您可以使用最新版本的RICHEDIT50W,而不是在RichTextBox中看到的,为此,您应该从标准RichTextBox继承并重写CreateParams,并将ClassName设置为RICHEDIT50W: 代码 using System; using System.ComponentModel; using System.Runtime.InteropServices;

是否有可能在用户选择后,每个选定的字母都显示其原始颜色?而且不总是默认的白色

我想实现这样的目标 你可以在写字板上看到


您可以使用最新版本的
RICHEDIT50W
,而不是在
RichTextBox
中看到的,为此,您应该从标准
RichTextBox
继承并重写
CreateParams
,并将
ClassName
设置为
RICHEDIT50W

代码

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;
        }
    }
}
屏幕截图

注意:

  • 多亏了这个古老而有用的VisualStudio工具,我可以使用Spy++看到写字板的RichTextBox类

  • 如果操作系统中的
    RICHEDIT50W
    有任何问题,可以打开
    Spy++
    WordPad
    ,然后选择它的
    RichTextBox
    ,查看类名

  • 当我搜索如何将
    RICHEDIT50W
    类应用于我的控件时,我找到了@Elmue的伟大帖子,多亏了他

对于任何对Visual Studio 2019有问题的人,以及Hunar评论中提到的异常“类已经存在”,我稍微修改了代码,只加载了一次库,这为我解决了问题

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[DesignerCategory("Code")]
public class RichTextBox5 : RichTextBox
{
    [DllImport("kernel32.dll", EntryPoint = "LoadLibraryW", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern IntPtr LoadLibraryW(string s_File);

    private static readonly object libraryLoadLock = new object();
    private static bool libraryLoadFlag;

    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
            {
                lock (libraryLoadLock)
                {
                    if (!libraryLoadFlag)
                    {
                        LoadLibrary("MsftEdit.dll"); // Available since XP SP1
                        libraryLoadFlag = true;
                    }
                }
                cp.ClassName = "RichEdit50W";
            }
            catch { /* Windows XP without any Service Pack.*/ }
            return cp;
        }
    }
}

很抱歉以这种方式回答,但由于信誉点数不足,我只能发布一个答案。

我知道热可以给不同的部分上色,但我希望在选择时看到它。
richTextBox1.SelectionColor=System.Drawing.color.Red也许当你选择颜色时,你会用它来改变。不,它不是这样工作的。如果没有选择,它将更改所选文本或当前插入符号位置的文本的颜色,但在选择时不会影响文本的颜色。您称之为哪个事件?也许你在挑选的时候没有提神。我想你不明白我的意思。我不需要调用事件,因为这只是我按编程方式进行的选择。所以我只是选择-仅此而已。更重要的是,没有必要提神。请仔细阅读我的问题。选择后更改选择文本效果良好。在选择时,选择文本的颜色始终为白色,就像在我附加的屏幕上一样。非常感谢!现在,选择时颜色的可见性非常好@Reza Aghaei,我该把这门课放在我的项目中的什么地方?因为我在多个表单中使用richtextbox?当您将类添加到项目中时,如果关闭所有设计器并重新生成项目,
ExRichText
将出现在工具箱中,您可以将其放到表单上。@Reza Aghaei,但当我将其拖动到用户控件时,会出现以下错误:,能告诉我怎么解决吗?RichEditBox的7.0或8.0版有什么好运气吗?