GDI + getting started (3. GDI + TEXT operations)

Source: Internet
Author: User

3. Text operations in GDI +

Using system. drawing;

Using system. Drawing. drawing2d;

Using system. Drawing. text;

Private void button#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 Pull la dskfj klaflka jdkla lkasdfj lsafj skladjf pull jlsdk pull glsjf Pull lksd jgfkl sdfkl pull la dsalf release, then 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 Pull la dskfj klaflka jdkla lkasdfj lsafj skladjf pull jlsdk pull glsjf Pull lksd jgfkl sdfkl pull la dsalf release, then 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 = 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, 100366f, 90.0f };

SF. settabstops (0.0f, TS );

 

String S1 = "\ tname \ Thair color \ Teys color \ theight ";

String S2 = "\ tbob \ tbrown \ t 175cm ";

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 = 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 );

}

}

Note:

Button#click: Learn the general format of G. drawstring

Button2_click: Learn the fontstyle enumerated values

Regular plain text.

Bold bold.

Italic skew text.

Underline underlined text.

Strikeout contains text passing through a straight line.

Font F6 = new font ("Times New Roman", 12, fontstyle. Underline | fontstyle. Bold | fontstyle. italic | fontstyle. Strikeout );

Requires understanding of the bitwise "or" Operator ("| ")

Button4_click: String Size Calculation

Measurestring: The measurement is drawn with the specified font object and formatted with the specified stringformat object.

Button5_click: Text alignment

Stringformat Sf = new stringformat ();

SF. Alignment = stringalignment. Center;

SF. linealignment = stringalignment. Center;

Near specifies that the text is aligned close to the layout. In the left-to-right layout, the position of the near end is left. In the right-to-left layout, the position near the end is right.

Center specifies the center alignment of text in the layout rectangle.

FAR specifies that the text is aligned away from the origin of the layout rectangle. In the left-to-right layout, the remote location is right. In the right-to-left layout, the remote location is left.

Button6_click: vertical text Effect

Button7_click: Table Effect

Button8_click: mesh Effect

Button9_click: query the fonts that have been installed in the system

Button10_click: another method for querying system fonts that have been installed

 

I am also learning GDI +, which is easy to write, making the experts laugh. Give me some advice.

Email: cosco.cheung@hotmail.com

QQ: 4631163

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.