Windows Forms 和控制項中的國際字型

在國際應用程式中,選取字型的建議方法是盡可能使用字型後援。 字型後援表示系統決定字元所屬的指令碼。

使用字型後援

若要利用這項功能,請勿設定表單或任何其他元素的 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);

另請參閱