最佳答案
您可以使用最新版本的RichTextBox
即 RICHEDIT50W
,为此您应该继承标准 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;
}
}
}
截图
注意:
多亏了这个古老而有用的 visual studio 工具,我可以使用 Spy++ 看到写字板的 RichTextBox 类。
如果您在操作系统中使用
RICHEDIT50W
有任何问题,您可以打开Spy++
和WordPad
然后选择RichTextBox
并查看类名是什么。
- 当我搜索有关将
RICHEDIT50W
类应用到我的控件时,我找到了 this @Elmue 的精彩帖子,感谢他。
关于c# - RichTextBox 选择突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32591157/