答案 0 :(得分:7)
您可以使用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;
}
}
}
<强>截图强>
注意:强>
由于这个古老有用的视觉工作室工具,我可以使用Spy ++看到wordpad的RichTextBox类。
如果您的操作系统中RICHEDIT50W
有问题,可以打开Spy++
和WordPad
,然后选择RichTextBox
,看看&# 39;是班级名称。
RICHEDIT50W
课程应用到我的控制中时,我到达this @Elmue的好帖子,谢谢他。答案 1 :(得分:1)
对于任何在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;
}
}
}
很抱歉以这种方式回答,但是由于声誉得分不足,我只能发布答案。