如何:使用 FontDialog 组件显示字体列表

FontDialog 组件允许用户选择字体,以及更改其显示方面,例如其粗细和大小。

对话框中选择的 Font 字体将在属性中返回。 因此,利用用户选择的字体与读取属性一样简单。

使用 FontDialog 组件选择字体属性

  1. 使用 ShowDialog 方法显示对话框。

  2. 使用 DialogResult 属性确定对话框的关闭方式。

  3. 使用属性 Font 设置所需的字体。

    在下面的示例中,Button 控件的 Click 事件处理程序将打开 FontDialog 组件。 选择字体后,用户单击 “确定”, Font 窗体上控件的属性 TextBox 将设置为所选字体。 该示例假定窗体有一个 Button 控件、一个 TextBox 控件和一个 FontDialog 组件。

    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
       If FontDialog1.ShowDialog() = DialogResult.OK Then
          TextBox1.Font = FontDialog1.Font
       End If
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       if(fontDialog1.ShowDialog() == DialogResult.OK)
       {
          textBox1.Font = fontDialog1.Font;
       }
    }
    
    private:
       void button1_Click(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          if(fontDialog1->ShowDialog() == DialogResult::OK)
          {
             textBox1->Font = fontDialog1->Font;
          }
       }
    

    (Visual C# 和 Visual C++)将以下代码置于表单的构造函数中以注册事件处理程序。

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    

另请参阅