界面:
功能:在上面的textbox中是由汉字的,然后点击下面的字体、字号还有字型,上面的字就能够根据要求变化。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace E
{
public partial class Form1 : Form
{
string myFontName;
float myFontSize;
FontStyle myFontStyle; //字体样式 FontStyle是方法
Font myFont;
public Form1()
{
InitializeComponent();
textBox1.Text="测绘系";
// \r: return,意思是回车,模拟打字机动作,从头开始(不换新行)
// \n: new line,意思是开始一个新行。
SetupFont();
}
private void SetupFont()
{
if (rdoFontNameSong.Checked==true) myFontName = "宋体";
if (rdoFontNameHua.Checked) myFontName = "华文琥珀";
if (rdoFontNameKai.Checked) myFontName = "楷体";
if (rdoFontSize5.Checked) myFontSize = 9;
if (rdoFontSize3.Checked) myFontSize = 15.75f;
if (rdoFontSize1.Checked) myFontSize = 26.25f;
//复选框需考虑所有可能的组合
if (chkFontBold.Checked && chkFontItalic.Checked && chkFontUnderline.Checked)
myFontStyle = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline; //按位或
else if (chkFontBold.Checked && chkFontItalic.Checked)
myFontStyle = FontStyle.Bold | FontStyle.Italic;
else if (chkFontBold.Checked && chkFontUnderline.Checked)
myFontStyle = FontStyle.Bold | FontStyle.Underline;
else if (chkFontItalic.Checked && chkFontUnderline.Checked)
myFontStyle = FontStyle.Italic | FontStyle.Underline;
else if (chkFontBold.Checked)
myFontStyle = FontStyle.Bold;
else if (chkFontItalic.Checked)
myFontStyle = FontStyle.Italic;
else if (chkFontUnderline.Checked)
myFontStyle = FontStyle.Underline;
else
myFontStyle = FontStyle.Regular;
//根据选定的字体名、字体大小、字体样式,创建一种新字体
myFont = new Font(myFontName, myFontSize, myFontStyle);
textBox1.Font = myFont;
}
private void rdoFontNameSong_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
private void rdoFontNameHua_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
private void rdoFontNameKai_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
private void rdoFontSize5_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
private void rdoFontSize3_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
private void rdoFontSize1_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
private void chkFontBold_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
private void chkFontItalic_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
private void chkFontUnderline_CheckedChanged(object sender, EventArgs e)
{
SetupFont();
}
}
}
实现起来和理解起来都比较简单,因为重点就是点击不同的选择之后发生的变化。