且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用较新的RichEdit版本?

更新时间:2023-12-02 21:34:40

我的计算机上具有RichEdit版本8.0,类名称为RICHEDIT60W.它存储在C:\ Program Files(x86)\ Common Files \ Microsoft Shared \ OFFICE15 \ RICHED20.DLL中.当我为它编写一个包装器时,它就可以正常工作:

I have RichEdit version 8.0 on my machine, class name RICHEDIT60W. It is stored in C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\RICHED20.DLL. It works just fine when I write a wrapper for it:

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标志,当您的计算机上没有Office 2013时,您必须对其进行更改.仅当您对要在其上运行程序的计算机拥有良好的控制权时,要求用户在其计算机上安装了正确的Office版本才能很好地工作.在技​​术上可以在LoadLibrary()失败时使用后备路径.

Not thoroughly tested. Hopefully you are very uncomfortable with this code, it is really only good enough for testing purposes to see if you are ahead at all. The path of the DLL is of course the Big Red flag, you'll have to change it when you don't have Office 2013 on your machine. Demanding that the user has the correct Office version installed on his machine only works well when you have decent control over the machines on which your program is going to run. Using a fall-back path when LoadLibrary() fails is technically possible.

此特定版本的功能以及与工具箱中默认RichTextBox不兼容的方式很难进行反向工程.粗略的猜测是与Word更加兼容".后来的RichEdit版本更好地支持数学方程式例如.您可以找到的唯一方法是进行全面测试.***坚持使用msftedit.dll

What this specific version does and how it might be incompatible with the default RichTextBox in the toolbox is pretty hard to reverse-engineer. The rough guess is "more compatible with Word". Later RichEdit versions better support math equations for example. Only way you can find out is by thoroughly testing. Best to stick with msftedit.dll