源代码:
namespace WindowsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnShowFontDialog_Click(object sender, EventArgs e)
{
//设置字体对话框实例
FontDialog dlg = new FontDialog();
//设置属性
dlg.AllowScriptChange = cbAllowScriptChange.Checked;
dlg.AllowSimulations = cbAllowSimulations.Checked;
dlg.AllowVectorFonts = cbAllowVectorFonts.Checked;
dlg.AllowVerticalFonts = cbAllowVerticalFonts.Checked;
dlg.FixedPitchOnly = cbFixedPitchOnly.Checked;
dlg.FontMustExist = cbFontMustExist.Checked;
dlg.ScriptsOnly = cbScriptsOnly.Checked;
dlg.ShowColor = cbShowColor.Checked;
dlg.ShowApply = cbShowApply.Checked;
dlg.ShowEffects = cbShowEffects.Checked;
dlg.ShowHelp = cbShowHelp.Checked;
dlg.MaxSize = int.Parse(txtMaxSize.Text);
dlg.MinSize = int.Parse(txtMinSize.Text);
//为字体对话框的Apply事件添加处理方法
dlg.Apply += new EventHandler(this.ApplyFontChange);
//显示对话框
if (dlg.ShowDialog() == DialogResult.OK)
{
//改变显示结果标签的字体和颜色
lblResult.Text = "测试字体效果";
lblResult.Font = dlg.Font;
lblResult.ForeColor = dlg.Color;
}
}
//字体对话框的Apply事件的处理方法
private void ApplyFontChange(object sender, System.EventArgs e)
{
FontDialog dlg = (FontDialog)sender;
lblResult.Font = dlg.Font;
lblResult.ForeColor = dlg.Color;
}
}
}
namespace WindowsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnShowFontDialog_Click(object sender, EventArgs e)
{
//设置字体对话框实例
FontDialog dlg = new FontDialog();
//设置属性
dlg.AllowScriptChange = cbAllowScriptChange.Checked;
dlg.AllowSimulations = cbAllowSimulations.Checked;
dlg.AllowVectorFonts = cbAllowVectorFonts.Checked;
dlg.AllowVerticalFonts = cbAllowVerticalFonts.Checked;
dlg.FixedPitchOnly = cbFixedPitchOnly.Checked;
dlg.FontMustExist = cbFontMustExist.Checked;
dlg.ScriptsOnly = cbScriptsOnly.Checked;
dlg.ShowColor = cbShowColor.Checked;
dlg.ShowApply = cbShowApply.Checked;
dlg.ShowEffects = cbShowEffects.Checked;
dlg.ShowHelp = cbShowHelp.Checked;
dlg.MaxSize = int.Parse(txtMaxSize.Text);
dlg.MinSize = int.Parse(txtMinSize.Text);
//为字体对话框的Apply事件添加处理方法
dlg.Apply += new EventHandler(this.ApplyFontChange);
//显示对话框
if (dlg.ShowDialog() == DialogResult.OK)
{
//改变显示结果标签的字体和颜色
lblResult.Text = "测试字体效果";
lblResult.Font = dlg.Font;
lblResult.ForeColor = dlg.Color;
}
}
//字体对话框的Apply事件的处理方法
private void ApplyFontChange(object sender, System.EventArgs e)
{
FontDialog dlg = (FontDialog)sender;
lblResult.Font = dlg.Font;
lblResult.ForeColor = dlg.Color;
}
}
}
FontDialog dlg = (FontDialog)sender;这句进行类型转换,还原为FontDialog类型。
FontDialog dlg = (FontDialog)sender;这句进行类型转换,还原为FontDialog类型。
如果要转换成的类型为引用类型,还可以用 as
显示转换
隐式转换
用户自定义转换