1. 程式人生 > >GDI+入門系列(三)——字型

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 saldkfjlalksdjf 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 saldkfjlalksdjf 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查詢系統已經安裝的字型