C#中的Winform停止显示unicode汉字 [英] Winform in C# stopped displaying unicode Chinese characters

查看:267
本文介绍了C#中的Winform停止显示unicode汉字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2010 C#Winform中,我有一个奇怪的Unicode字符显示行为。我正在使用Windows 7 x64。出于某种原因,我能够在代码中做这样的事情,并按下按钮时,字符显示正确。字体是默认的微软无衬线字体。

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

然而,在关闭项目并重新打开按钮之后只显示2个方块,就好像字符丢失一样。我不明白什么可以改变,为什么它第一次工作。

有些职位建议使用MS哥特式的控制,但由于某些原因,我可以'即使在属性窗口中选择。字体在我的系统中,因为我检查控制面板 - >字体。 MS哥特式在那里。



是否有一些设置可以改变winform中的字体设置,以允许MS Gothic字体?

我现在可以使用的唯一方法是重写按钮的绘制事件,然后在那里进行渲染。虽然这对DataGrid等组件来说比较困难。这是用于带有中文字符的按钮的代码:

prerivate $ _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 =初始化系统初始化系统;
btnIntize.Text = string.Empty;
e.Graphics.DrawString(text,btn.Font,drawBrush,e.ClipRectangle,sf);
drawBrush.Dispose();
sf.Dispose();


解决方案

汉字而不是日文,正确的字体是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.

这篇关于C#中的Winform停止显示unicode汉字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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