用户可以使用 FontDialog 组件选择字体,并可以更改字体显示方式,例如粗细和大小。
在该对话框中选择的字体在 Font 属性中返回。 因此,使用用户选定的字体就像读取属性一样简单。
使用 FontDialog 组件选择字体属性
使用 ShowDialog 方法显示对话框。
使用 DialogResult 属性确定对话框是如何关闭的。
使用 Font 属性设置所需的字体。
在下面的示例中,Button 控件的 Click 事件处理程序打开一个 FontDialog 组件。 当用户选定字体并单击**“确定”**时,窗体上的 TextBox 控件的 Font 属性被设置为选定的字体。 本示例假定窗体上有一个 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);