Windows 窗体和控件中的国际字体

在国际化应用程序中,建议尽量使用字体后备来选择字体。 字体后备意味着由系统确定字符属于哪个脚本。

使用字体后备

若要利用此功能,请不要为窗体或任何其他元素设置 Font 属性。 应用程序将自动使用默认系统字体,该字体会因操作系统的不同本地化语言而异。 当应用程序运行时,系统会自动为操作系统中选择的区域性提供正确的字体。

不设置字体的规则例外,这是用于更改字体样式。 对于用户单击按钮以使文本框中的文本以粗体显示的应用程序来说,这一点可能很重要。 为此,将编写一个函数,以根据窗体的字体是什么,将文本框的字体样式更改为粗体。 必须在两个位置调用此函数:在按钮的 Click 事件处理程序和 FontChanged 事件处理程序中。 如果函数仅在 Click 事件处理程序中调用,而其他一些代码段会更改整个窗体的字体系列,则文本框不会随窗体的其余部分更改。

Private Sub MakeBold()
   ' Change the TextBox to a bold version of the form font
   TextBox1.Font = New Font(Me.Font, FontStyle.Bold)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   ' Clicking this button makes the TextBox bold
   MakeBold()
End Sub

Private Sub Form1_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.FontChanged
   ' If the TextBox is already bold and the form's font changes,
   ' change the TextBox to a bold version of the new form font
   If (TextBox1.Font.Style = FontStyle.Bold) Then
      MakeBold()
   End If
End Sub
private void button1_Click(object sender, System.EventArgs e)
{
   // Clicking this button makes the TextBox bold
   MakeBold();
}

private void MakeBold()
{
   // Change the TextBox to a bold version of the form's font
   textBox1.Font = new Font(this.Font, FontStyle.Bold);
}

private void Form1_FontChanged(object sender, System.EventArgs e)
{
   // If the TextBox is already bold and the form's font changes,
   // change the TextBox to a bold version of the new form font
   if (textBox1.Font.Style == FontStyle.Bold)
   {
      MakeBold();
   }
}

但是,本地化应用程序时,某些语言的粗体字体可能会显示得很差。 如果这是个问题,则希望本地化人员可以选择将字体从粗体切换到常规文本。 由于本地化人员通常不是开发人员,并且无权访问源代码,而只能访问资源文件,因此需要在资源文件中设置此选项。 为此,请将 Bold 属性设置为 true。 这会导致字体设置被写出到资源文件,本地化人员可在其中编辑它。 然后,你可以在 InitializeComponent 方法之后编写代码,根据窗体的字体(但使用资源文件中指定的字体样式)重置字体。

TextBox1.Font = New System.Drawing.Font(Me.Font, TextBox1.Font.Style)
textBox1.Font = new System.Drawing.Font(this.Font, textBox1.Font.Style);

另请参阅