三、GDI+中文字操作
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
FontFamily ff = new FontFamily("Times New Roman");
Font f = new Font(ff, 12);
String s = "Heigth:" + f.Height;
SizeF sf = g.MeasureString(s, f, Int32.MaxValue, StringFormat.GenericTypographic);
RectangleF r = new RectangleF(0, 0, sf.Width, f.Height);
g.DrawRectangle(Pens.Black, r.Left, r.Top, this.Width, r.Height);
g.DrawString(s, f, Brushes.Black, r, StringFormat.GenericTypographic);
f.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f1 = new Font("Times New Roman", 12, FontStyle.Regular);
Font f2 = new Font("Times New Roman", 12, FontStyle.Bold);
Font f3 = new Font("Times New Roman", 12, FontStyle.Italic);
Font f4 = new Font("Times New Roman", 12, FontStyle.Strikeout);
Font f5 = new Font("Times New Roman", 12, FontStyle.Underline);
Font f6 = new Font("Times New Roman", 12, FontStyle.Underline | FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout);
int h = f1.Height;
g.DrawString("hello word", f1, Brushes.Black, 0, 0);
g.DrawString("hello word", f2, Brushes.Black, 0, h);
g.DrawString("hello word", f3, Brushes.Black, 0, 2 * h);
g.DrawString("hello word", f4, Brushes.Black, 0, 3 * h);
g.DrawString("hello word", f5, Brushes.Black, 0, 4 * h);
g.DrawString("hello word", f6, Brushes.Black, 0, 5 * h);
f1.Dispose();
f2.Dispose();
f3.Dispose();
f4.Dispose();
f5.Dispose();
f6.Dispose();
}
private void button4_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "adsfalk kldjf sdafkl saldkfjla lksdjf la dskfj klaflka jdkla lkasdfj lsafj skladjf lsakjdflsa jlsdk fklsdajfklsjgkl glsjf lsfklsjalkdf skldafjsldgj lksd jgfkl sdfkl fsdakljdf la dsalf lakjfklgjoi ,kfgkldjsagnncvsdf sdf sa";
Font f = new Font("Arial", 12, FontStyle.Bold | FontStyle.Strikeout);
SizeF sf = g.MeasureString(s, f, 200);
RectangleF rf = new RectangleF(20, 20, sf.Width, sf.Height);
g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);
g.DrawString(s, f, Brushes.Black, rf);
}
private void button5_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "adsfalk kldjf sdafkl saldkfjla lksdjf la dskfj klaflka jdkla lkasdfj lsafj skladjf lsakjdflsa jlsdk fklsdajfklsjgkl glsjf lsfklsjalkdf skldafjsldgj lksd jgfkl sdfkl fsdakljdf la dsalf lakjfklgjoi ,kfgkldjsagnncvsdf sdf sa";
Font f = new Font("Arial", 12);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Rectangle r = new Rectangle(0, 0, 300, f.Height * 8);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, f, Brushes.Black, r, sf);
f.Dispose();
sf.Dispose();
g.Dispose();
}
private void button6_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "Accring Stanley";
StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);
Font f = new Font("Times New Roman", 14);
SizeF sizef = g.MeasureString(s, f, Int32.MaxValue, sf);
RectangleF rf = new RectangleF(20, 20, sizef.Width, sizef.Height);
g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);
g.DrawString(s, f, Brushes.Black, rf, sf);
f.Dispose();
}
private void button7_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f = new Font("Times New Roman", 12);
Font bf = new Font(f, FontStyle.Bold);
StringFormat sf = new StringFormat();
float[] ts = { 10.0f, 70.0f, 100.0f, 90.0f };
sf.SetTabStops(0.0f, ts);
string s1 = "\tName\tHair Color\tEys Color\tHeight";
string s2 = "\tBob\tBrown\tBrown\t175cm";
g.DrawString(s1, bf, Brushes.Black, 20, 20, sf);
g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf);
f.Dispose();
bf.Dispose();
}
private void button8_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f = new Font("Times New Roman", 48, FontStyle.Bold);
HatchBrush hb = new HatchBrush(HatchStyle.Cross, Color.White, Color.Black);
g.DrawString("Ctazy Crosshatch", f, hb, 0, 0);
f.Dispose();
}
private void button9_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
FontFamily[] fontFamilies;
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
fontFamilies = installedFontCollection.Families;
for (int i = 0; i < fontFamilies.Length; ++i)
{
Console.WriteLine("FontFamily name:" + fontFamilies[i].Name);
}
}
private void button10_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, this.ClientRectangle);
FontFamily[] families = FontFamily.GetFamilies(g);
for (int i = 0; i < families.Length; ++i)
{
Console.WriteLine("FontFamily name:" + families[i].Name);
}
}
说明:
button1_Click:学会g.DrawString的一般使用格式
button2_Click:学会FontStyle枚举值
Regular 普通文本。
Bold 加粗文本。
Italic 倾斜文本。
Underline 带下划线的文本。
Strikeout 中间有直线通过的文本。
Font f6 = new Font("Times New Roman", 12, FontStyle.Underline | FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout);
要求了解按位“或”运算符(“|”)
button4_Click:字符串大小计算
MeasureString:测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串
button5_Click:文字对齐方式
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Near 指定文本靠近布局对齐。在左到右布局中,近端位置是左。在右到左布局中,近端位置是右。
Center 指定文本在布局矩形中居中对齐。
Far 指定文本远离布局矩形的原点位置对齐。在左到右布局中,远端位置是右。在右到左布局中,远端位置是左。
button6_Click:做文字竖直效果
button7_Click:表格效果
button8_Click:网格效果
button9_Click:查询系统已经安装的字体
Button10_Click:查询系统已经安装的字体的另一种写法
本人也在学习GDI+,写得比较简单,让高手见笑了。欢迎高手给我指点
QQ:4631163