RichTextBox cannot display Unicode Mathematical alphanumeric symbols

*爱你&永不变心* 提交于 2019-11-27 18:23:09

问题


I cannot get WinForms RichTextBox display some Unicode characters, particularly Mathematical alphanumeric symbols (but the problem is most probably not limited to those).

Surprisingly the same characters can display in a plain or multiline TextBox using the same (default) font. Even if I change the font to for example "Arial" or "Lucida", I get the same results.

The screenshot is from Windows 10, but I'm getting the same results on Windows 7. The example shows ascii small a-d followed by mathematical italic sans-serif small alpha-delta.

I'm using Visual Studio 2017 and .NET 4.6.1.

A trivial test code:

private void InitializeComponent()
{
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    // ...
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(25, 38);
    this.textBox1.Multiline = true;
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(182, 108);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "abcd 𝛼𝛽𝛾𝛿";
    // 
    // richTextBox1
    // 
    this.richTextBox1.Location = new System.Drawing.Point(213, 38);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.Size = new System.Drawing.Size(179, 108);
    this.richTextBox1.TabIndex = 1;
    this.richTextBox1.Text = "abcd 𝛼𝛽𝛾𝛿";
    // ...
}

Note that it does not seem to be a problem with storing the characters. The characters are correctly stored in the RichTextBox. If you copy the text out and paste it somewhere (like to the TextBox), all characters display correctly.

On the other hand, if you paste the characters to the RichTextBox, you get the same incorrect display.

So it looks like a display problem only.


回答1:


It's a bug/decision by design in RichTextBox which has been fixed in .NET 4.7.

RichTextBox is in fact a wrapper around RichEdit. In .NET 4.7, the control is using RICHEDIT50W while in previous versions it's using RichEdit20W.

To solve the problem you can use either of these options:

  • Upgrade to .NET 4.7

OR

  • You can use the latest version of RichTextBox that is RICHEDIT50W, to do so you should inherit from standard RichTextBox and override CreateParams and load library Msftedit.dll and set the ClassName to RICHEDIT50W.

To see an implementation, take a look at this post.



来源:https://stackoverflow.com/questions/47433656/richtextbox-cannot-display-unicode-mathematical-alphanumeric-symbols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!