This article has been excerpted from book "Graphics Programming with GDI+".
The font class combines a font and methods and properties to define functionalities such as a font size, style, name and conversion. Table 5.8 describes the properties of the Font class.
The Following code creates a font object of font family Arial with size 16 and uses the Font class properties to find out the details of the Font object.
Font arialFont = new Font (" Arial ", 16, FontStyle.Bold|FontStyle.Underline|FontStyle.Italic);
MessageBox.Show(" Font Properties = Name: " + arialFont.Name + " Size: " + arialFont.Size.ToString() + " Style :"+ arialFont.Style.ToString() + " Default Unit:" + arialFont.Unit.ToString() + "Size in points:"+ arialFont.SizeInPoints.ToString());
The Font class provide three static methods: FromHdc, FromHfont, and FromLogFont. These methods create a Font object from a window handle to a device context, a window handle and GDI LOGFONT structure, respectively. The GetHeight method returns the height of a Font object. The ToHfont and ToLogFont methods convert a Font object to window handler and GDI LOGFONT structure, respectively.
TABLE 5.8 Font properties
Property |
Description |
Bold |
Return true if the font is bold. |
FontFamily |
Every font belongs to s font family. This property return the FontFamily object associated with a Font object. |
GdiCharSet |
Return a string containing all characters. |
GdiVerticalFont |
Returns true if a font is derived from a GDI vertical font; Otherwise return false. |
Height |
Return the height of a font. |
Italic |
Return true if font is italic. |
Name |
Return the face name off font. |
Size |
Return the em size of a font in font design units. |
SizeInPoints |
Return size, in points of a font. |
Strickout |
Return true if a font specifies a horizontal line through the font. |
Style |
Return style information for a font, which is a type FontStyle enumeration. |
Underline |
Return true if font is underlined. |
Unit |
Return the unit of measure for a font. |
In the following example, you must import the GDI library by adding the following code at the beginning of your class before using any GDI fonts, because we will be using GetStockObject:
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtrn GetStockobject(int fnobj);
Listing 5.7 creates a font from a GDI handle and draws a string on the form. The FromHfont method creates a Font object from a GDI handle.
LISTING 5.7 Using the FromHfont method
private void FromHfontmenu_Click(object sender, System.EventArgs e)
{
//Create the Graphics object
Graphics g = this.CreateGraphics();
//Create a brush
SolidBrush brush = new SolidBrush(Color.Red);
//Get a handle
Intptr hFont = GetStockobject(o);
//Create a font from the handle A
Font hfontFont = Font.FromHfont(hfont);
//Draw text
g.DrawString("GDI HFONT",hfontFont,brush,20,20);
//Dipose of objects
hfontFont.Dispose();
g.Dispose();
}
Figure 5.12 shows the output from Listing 5.7
FIGURE 5.12 Using the FromHFont method
Conclusion
Hope the article would have helped you in understanding Font Class in GDI+. Read other articles on GDI+ on the website.
|
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows. |