Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

c# - Windows.Forms.RichTextBox Loses table background colours

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.

Am I missing something when I load the file into the Windows Forms 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 );
            }
        }
    }

In the above code snippet I have also tried using

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

and

richTextBox1.LoadFile(fDialog.FileName);

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 : enter image description here

Thanks in advance for any help.

Steve.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

A sample table I created with Word rendered perfectly:

enter image description here


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...