且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中的Winform停止显示unicode汉字

更新时间:2023-01-31 09:18:29

汉字而不是日文,正确的字体是SimSun或Ms Song。微软有时会自动为您自动切换字体,例如,当您将初始化文本复制粘贴到Word中时,您会看到英文部分是以Arial或像中文文本被自动切换到SimSun或任何默认的中文字体在您的计算机上。我想这是当文本正确显示时发生的事情。然后,在保存项目时出现一个按钮标题不能用两种不同的字体的问题,所以它保存了微软Sans Serif的按钮字体,现在正在为你提供方框。解决方案是使用SimSun或Ms Song作为按钮,因为这些字体可以同时显示英文和中文字符。如果您想要同样的按钮也显示德语或韩语等,您将遇到一个问题。如果是这样的话,您将需要根据本地化语言动态设置按钮字体。


I have a strange Unicode character displaying behavior in Visual Studio 2010 C# Winform. I'm using Windows 7 x64. For some reason I was able to do something like this in the code and got the character to display correctly when pressing the button. The font was the default Microsoft Sans Serif.

  private void button1_Click(object sender, EventArgs e)
  {
    button1.Text = "Initialize System 初始化系统";
  }

However, after I closed down the project and re-open it the button now only displays 2 squares as if the characters are missing. I don't understand what could have changed and why it had worked the first time.

Some posts suggested using MS Gothic for the control, but for some reasons I can't even choose that in the properties window. The font is in my system since I checked in control panel -> Font. MS Gothic was there.

Is there some settings that could have changed the font settings in the winform to allow MS Gothic font?

The only way I could get this to work now is to override the paint event of the button and then do the rendering there. This is more difficult to do on component like DataGrid though. This is the code that works for the button with Chinese characters:

private void _Paint(object sender, PaintEventArgs e)
{
  Button btn = (Button)sender;
  SolidBrush drawBrush = new SolidBrush(btn.ForeColor);
  StringFormat sf = new StringFormat { Alignment = StringAlignment.Center,
                                       LineAlignment = StringAlignment.Center };
  string text = "Initialize System 初始化系统";
  btnIntialize.Text = string.Empty;
  e.Graphics.DrawString(text, btn.Font, drawBrush, e.ClipRectangle, sf);
  drawBrush.Dispose();
  sf.Dispose();
}

As you're using Simplified Chinese characters and not Japanese, the correct font is SimSun or Ms Song.

Microsoft sometimes automatically switches the font for you, for example when you copy-paste that initializing text into Word you'll see that the English part is written in Arial or the like and the Chinese text was automatically switched to SimSun or whatever is the default Chinese font on your computer. I imagine this is what happened when the text was displayed correctly at first. Then, when saving the project it had the problem that one button caption cannot be in two different fonts, so it saved the button font as Microsoft Sans Serif and is now giving you square boxes because of that. The solution is to use SimSun or Ms Song for the button, because these fonts can display both English and Chinese characters. You will encounter a problem if you want the same button to also display German or Korean or the like though. If that is the case, you will need to dynamically set the button font depending on the localization language.