Windows.Forms.RichTextBox Loses the background color of the table

When you load an RTF file into RichTextBox Windows Forms, it loses the background color of the table cells. If we use WPF RichTextBox and load the same file, everything will be formatted as it should.

Am I missing something when uploading a file to 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 also tried to use

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

as well as

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 a screenshot from both versions: enter image description here

Thank you in advance for any help.

Steve.

+5
source share
1 answer

RichTextBox, Winforms, , 2.0. .NET 1.x .NET 2.0, , Windows, 98. v2.0 .

, . 5.0 XP . , , 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


: Winforms, 4.7, .

+7

Source: https://habr.com/ru/post/1620764/


All Articles