c# - Windows.Forms.RichTextBox 丢失表格背景颜色

标签 c# .net wpf winforms richtextbox

将 rtf 文件加载到 Windows 窗体 RichTextBox 时,它会丢失表格单元格的背景颜色。如果我们使用 WPF RichTextBox 并加载相同的文件,则所有内容都按应有的格式设置。

当我将文件加载到 Windows 窗体 RichTextBox 中时,是否遗漏了什么?

Windows 窗体 RichTextBox 代码片段:

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();
        fDialog.Filter = "Rich Text Files (*.rtf)|*.rtf";
        fDialog.Multiselect = false;
        fDialog.RestoreDirectory = true;
        if (fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if (fDialog.FileName != "")
            {
                richTextBox1.LoadFile(fDialog.FileName, RichTextBoxStreamType.RichText );
            }
        }
    }

在上面的代码片段中我也尝试过使用

richTextBox1.Rtf = File.ReadAllText(fDialog.FileName);

richTextBox1.LoadFile(fDialog.FileName);

WPF RichTextBox 代码片段

    private void load_file_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.OpenFileDialog fDialog = new System.Windows.Forms.OpenFileDialog();
        fDialog.Filter = "Rich Text Files (*.rtf)|*.rtf";
        fDialog.Multiselect = false;
        fDialog.RestoreDirectory = true;
        if (fDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if (fDialog.FileName != "")
            {
                FileStream fStream;
                fStream = new FileStream(fDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);

                richtextbox1.SelectAll();
                richtextbox1.Selection.Load(fStream, DataFormats.Rtf);
                fStream.Close();
            }
        }

    }

这是两个版本的屏幕截图: enter image description here

在此先感谢您的帮助。

史蒂夫。

最佳答案

RichTextBox 有很多版本,Winforms 锁定在早期版本 2.0。回到 .NET 1.x 和 .NET 2.0,这些版本仍然可以在 98 等古老的 Windows 版本上运行。缺少对 v2.0 中表的支持。

这是非常可修复的,升级版本不需要太多代码。 5.0 版可在 XP 及更高版本上使用。您所要做的就是加载 native DLL,msftedit.dll 而不是 riched20.dll,这样“RichEdit50W”窗口类就可用了。并覆盖 CreateParams 以使用该类。

向您的项目添加一个新类并粘贴如下所示的代码。编译。您可以从工具箱顶部放下新控件,替换旧控件。

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

public class RichTextBox5 : RichTextBox {
    protected override CreateParams CreateParams {
        get {
            if (moduleHandle == IntPtr.Zero) {
                moduleHandle = LoadLibrary("msftedit.dll");
                if ((long)moduleHandle < 0x20) throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not load Msftedit.dll");
            }
            var cp = base.CreateParams;
            cp.ClassName = "RichEdit50W";
            return cp;
        }
    }
    private static IntPtr moduleHandle;

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

我用 Word 完美呈现的示例表格:

enter image description here


更新:此代码现在是 built-in到 Winforms,目标至少是版本 4.7 以利用它。

关于c# - Windows.Forms.RichTextBox 丢失表格背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358088/

相关文章:

c# - C# 生成的 Outlook 电子邮件中的换行符/回车符

.net - 当用于自赋值时,编译器是否优化了 Null Coalescing 运算符?

c# - Parallel.For中断

wpf - 聚焦TextBox时设置ViewModel属性

c# - 按图像路径在数据库中的 RDLC 报告上显示图像

c# - 你调用的对象是空的。 -资源.resx

c# - 通过名称获取类成员

c# - "A reference to a volatile field will not be treated as volatile"含义

c# - 如何在 Silverlight 中以编程方式填充网格?

c# - 如何结合 Reverse Poco Generator 使用 Effort