使用较新的 RichEdit 版本?

Using newer RichEdit versions?

我试过在 C# 上使用 RichTextBox,发现它对于数千行长的文本来说太慢了。经过一些谷歌搜索,我发现这是因为 .net 默认使用 RichEdit 2.0,解决方案是改用 RichEdit 5.0。

C# RichEditBox has extremely slow performance (4 minutes loading) SOLVED

它工作正常,文本显示在几秒钟而不是几分钟内。然而,作为那种在个人项目中不关心兼容性的人,我想找到更高版本的 RichEdit。我发现最新版本是 8.0,整个发布为 riched20.dll,部分发布为 msftedit.dll。

http://blogs.msdn.com/b/murrays/archive/2006/10/14/richedit-versions.aspx

http://blogs.msdn.com/b/murrays/archive/2012/03/03/richedit-8-0-preview.aspx

但是,msdn 上的文档以 4.1 停止,(我假设是)该项目的开发人员声称他们不再在上面的博客中做 public 文档。

https://msdn.microsoft.com/en-us/library/windows/desktop/bb787873(v=vs.85).aspx

到目前为止,我已经能够 运行 msftedit.dll 明确地使用 RichEdit 2.0 和 5.0,但所有其他版本都让我望而却步。例如,尽管 John Crenshaw 的评论声称 RichEdit 6.0 工作正常,但我无法使用它。除上述 msftedit-2.0 和 5.0 组合之外的任何尝试都会在 Application.Run() 处导致 "Window class name is not valid" 错误。 (该程序是在 C# 中,但我没有标记它,因为我担心这个问题可能不是 C# 特定的问题。)该代码是第一个 link 中解决方案的近乎精确的副本,并且是如下:

class Textbox : 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)
{
    IntPtr h_Module = LoadLibraryW(s_File);
    if (h_Module != IntPtr.Zero)
        return h_Module;

    int s32_Error = Marshal.GetLastWin32Error();
    throw new Exception("LoadLibrary Failed with: "+s32_Error);
}

protected override CreateParams CreateParams
{
    get
    {
        CreateParams i_Params = base.CreateParams;
        try
        {
            // Available since XP SP1
            LoadLibrary("MsftEdit.dll"); // throws

            i_Params.ClassName = "RichEdit50W";
        }
        catch
        {
            // Windows XP without any Service Pack.
        }
        return i_Params;
    }
}

我所做的是将 ClassName 字符串更改为不同的数字,例如RichEdit60W.

我用的是Windows8.1,所以msftedit.dll应该是RichEdit 7.0或者8.0(博文post写的不清楚),但是我做不到到达他们。有什么办法可以纠正这个问题,或者新版本是否保密?

RichEdit 似乎主要是作为 Office 的一部分在 Microsoft 开发的,Windows 在不同时期仅包含版本 1.0、2.0、3.0 和 4.1。

其他更高版本的 RichEdit 可以在 Microsoft Office 安装中找到:如果安装了 Office,则必须从 "Program Files" 下的位置显式地 LoadLibrary() 它们。如果没有安装 Office,那你就倒霉了:这些其他版本不存在 Windows,并且没有再分发许可证允许你使用你编写的任何适当的内容分发它们。

所以,基本上,你运气不好。对不起。

我的机器上有 RichEdit 8.0 版,class 名称为 RICHEDIT60W。它存储在 C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\RICHED20.DLL 中。当我为它写一个包装器时它工作得很好:

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

class RichEdit80 : RichTextBox {
    protected override CreateParams CreateParams {
        get {
            if (moduleHandle == IntPtr.Zero) {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86);
                path = System.IO.Path.Combine(path, @"Microsoft Shared\OFFICE15\RICHED20.DLL");
                moduleHandle = LoadLibrary(path);
                if ((long)moduleHandle < 0x20) throw new Win32Exception(Marshal.GetLastWin32Error(), "RichEdit control appears to be missing");
            }
            CreateParams createParams = base.CreateParams;
            createParams.ClassName = "RichEdit60W";
            if (this.Multiline) {
                if (((this.ScrollBars & RichTextBoxScrollBars.Horizontal) != RichTextBoxScrollBars.None) && !base.WordWrap) {
                    createParams.Style |= 0x100000;
                    if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None) {
                        createParams.Style |= 0x2000;
                    }
                }
                if ((this.ScrollBars & RichTextBoxScrollBars.Vertical) != RichTextBoxScrollBars.None) {
                    createParams.Style |= 0x200000;
                    if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None) {
                        createParams.Style |= 0x2000;
                    }
                }
            }
            if ((BorderStyle.FixedSingle == base.BorderStyle) && ((createParams.Style & 0x800000) != 0)) {
                createParams.Style &= -8388609;
                createParams.ExStyle |= 0x200;
            }
            return createParams;
        }
    }

    private static IntPtr moduleHandle;

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr LoadLibrary(string lpFileName);
}

未经过彻底测试。希望你对这段代码感到非常不舒服,它真的只够用于测试目的,看看你是否领先。 DLL的路径当然是Big Red flag,如果你的机器上没有Office 2013,你就得更改它。要求用户在他的机器上安装正确的 Office 版本只有在你对你的程序将要运行的机器有适当的控制时才有效 运行。在 LoadLibrary() 失败时使用回退路径在技术上是可行的。

这个特定版本的功能以及它如何与工具箱中的默认 RichTextBox 不兼容很难进行逆向工程。粗略的猜测是"more compatible with Word"。 Later RichEdit versions 更好地支持数学方程式等。唯一可以找出答案的方法是彻底测试。最好坚持 msftedit.dll