操作字体选择对话框:
字体是图形编程的一个重要组成部分,通过设置不同的字体,你可以在程序中达到不同的视觉效果。和以上的颜色选择对话框的创建差不多,你可以很方便地创建一个字体选择对话框,并通过它来让用户选择其所需的字体。
下面同样给出一个实例,这个实例和上面的实例差不多,只是用来字体选择对话框代替了原来的颜色选择对话框,最后是根据用户的字体选择来设置文本框的字体。
using System;
using System.Drawing;
using System.Windows.Forms;
public class Fonts:Form {
Button b1 = new Button();
TextBox tb = new TextBox();
FontDialog flg = new FontDialog();
public Fonts() {
b1.Click += new EventHandler(b1_click);
b1.Text = "选择字体";
tb.Location = new Point(50,50);
this.Controls.Add(b1);
this.Controls.Add(tb);
}
public void b1_click(object sender, EventArgs e) {
clg.ShowDialog();
tb.FontName = flg.Font;
}
public static void Main() {
Application.Run(new Fonts());
}
}
using System.Drawing;
using System.Windows.Forms;
public class Fonts:Form {
Button b1 = new Button();
TextBox tb = new TextBox();
FontDialog flg = new FontDialog();
public Fonts() {
b1.Click += new EventHandler(b1_click);
b1.Text = "选择字体";
tb.Location = new Point(50,50);
this.Controls.Add(b1);
this.Controls.Add(tb);
}
public void b1_click(object sender, EventArgs e) {
clg.ShowDialog();
tb.FontName = flg.Font;
}
public static void Main() {
Application.Run(new Fonts());
}
}
