我下载了写字板的源代码 (http://download.microsoft.com/download/4/0/9/40946FEC-EE5C-48C2-8750-B0F8DA1C99A8/MFC/ole/wordpad.zip.exe),它的性能同样最差(4 分钟)。但是这个示例是旧版本的写字板。
因此,微软在过去几年中改进了写字板中 .NET 框架中缺少的任何内容。
终于找到了解决办法:
.NET 框架使用 RichEdit20W 类作为 Richedit 控件,就像旧的写字板一样。但是Windows XP的写字板使用的是经过微软高度改进的新RichEdit50W。
那么我如何告诉 .NET 框架使用 RichEdit50W 而不是 RichEdit20W 呢?
这很简单:从 RichTextBox 派生一个类并为 LoadLibary 编写一个托管包装器。
RichEdit50W 类是由 MsftEdit.dll 创建的,该文件从 Windows XP SP1 开始可用。我实施了对 RichEdit20W 的回退,以解决在没有服务包的情况下仍应使用 XP 的极少数情况。
而且它有效!
/// <summary>
/// The framework uses by default "Richedit20W" in RICHED20.DLL.
/// This needs 4 minutes to load a 2,5MB RTF file with 45000 lines.
/// Richedit50W needs only 2 seconds for the same RTF document !!!
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams i_Params = base.CreateParams;
try
{
// Available since XP SP1
Win32.LoadLibrary("MsftEdit.dll"); // throws
// Replace "RichEdit20W" with "RichEdit50W"
i_Params.ClassName = "RichEdit50W";
}
catch
{
// Windows XP without any Service Pack.
}
return i_Params;
}
}
注意:另请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/bb787873%28v=vs.85%29.aspx
public class Win32
{
[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)
{
IntPtr h_Module = LoadLibraryW(s_File);
if (h_Module != IntPtr.Zero)
return h_Module;
int s32_Error = Marshal.GetLastWin32Error();
throw new Win32Exception(s32_Error);
}
}