Windows.Forms.RichTextBox丢失表背景色 [英] Windows.Forms.RichTextBox Loses table background colours

查看:78
本文介绍了Windows.Forms.RichTextBox丢失表背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When loading a rtf file into a Windows Forms RichTextBox it loses the background colour of table cells. If we use a WPF RichTextBox and load the same file everything is formatted as it should.

当我将文件加载到Windows窗体RichTextBox中时,我会丢失某些东西吗?

Am I missing something when I load the file into the Windows Forms RichTextBox?

Windows窗体RichTextBox代码段:

Windows Forms RichTextBox code snippet :

    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 );
            }
        }
    }

在上面的代码片段I中还尝试使用

In the above code snippet I have also tried using

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

richTextBox1.LoadFile(fDialog.FileName);

WPF RichTextBox代码段

WPF RichTextBox code snippet

    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();
            }
        }

    }

两种版本的屏幕截图:

Here is the screen shot from both versions :

在此先感谢您的帮助。

史蒂夫。

推荐答案

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

There were many versions of RichTextBox, Winforms locked-in an early release, version 2.0. Goes back to .NET 1.x and .NET 2.0, versions that could still run on ancient Windows versions like 98. Support for tables in v2.0 is lacking.

这是可以修复的,升级版本不需要太多代码。 XP或更高版本提供5.0版。您所需要做的就是加载本机DLL msftedit.dll而不是riched20.dll,以便 RichEdit50W窗口类可用。并重写CreateParams以使用该类。

That's eminently fixable, it doesn't take much code to upgrade the version. Version 5.0 is available on XP and up. All you have to do is load the native DLL, msftedit.dll instead of riched20.dll, so that the "RichEdit50W" window class becomes available. And override CreateParams to use that class.

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

Add a new class to your project and paste the code shown below. Compile. You can drop the new control from the top of the toolbox, replacing the old one.

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创建的示例表完美呈现:

A sample table I created with Word rendered perfectly:

更新:this代码现在内置升级到Winforms,至少要定位4.7版才能使用它。

UPDATE: this code is now built-in to Winforms, target at least version 4.7 to take advantage of it.

这篇关于Windows.Forms.RichTextBox丢失表背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆