Drawing Text in VS .NET

Drawing Code-128 in VS .NET Drawing Text
121 Drawing Text
Generate Code 128 In .NET Framework
Using Barcode encoder for .NET framework Control to generate, create Code 128C image in .NET framework applications.
TABLE 121: Text-Drawing Features and Sample Programs (Continued)
Scan Code 128 In VS .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications.
Feature
Make Bar Code In VS .NET
Using Barcode drawer for .NET Control to generate, create barcode image in .NET applications.
Comment
Barcode Scanner In VS .NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
Sample Program
Drawing Code 128C In Visual C#.NET
Using Barcode creator for .NET Control to generate, create Code128 image in .NET framework applications.
MeasureString shows how to use results from the MeasureString method in drawing
Making USS Code 128 In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create ANSI/AIM Code 128 image in ASP.NET applications.
Calculate size of graphical text
Encode USS Code 128 In VB.NET
Using Barcode encoder for VS .NET Control to generate, create USS Code 128 image in VS .NET applications.
Optimal positioning of text requires calculating the size of the bounding box of drawn text This is accomplished using the MeasureString method By default, text is aligned to the upper-left corner of the text box Override this default by setting alignment properties in the StringFormat structure and calling the DrawString overrides which accept a StringFormat as a parameter
Code 128A Generator In .NET Framework
Using Barcode maker for VS .NET Control to generate, create Code 128 image in .NET framework applications.
Setting text alignment
Barcode Maker In VS .NET
Using Barcode printer for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
TextAlign shows nine
Make Barcode In VS .NET
Using Barcode creation for Visual Studio .NET Control to generate, create barcode image in .NET applications.
ways to align text by mixing and matching three vertical alignments and three horizontal alignments
Encoding USS Code 39 In .NET
Using Barcode maker for .NET framework Control to generate, create Code 39 Extended image in .NET framework applications.
1212 The DrawString Method All Compact Framework text drawing is done with the DrawString method, a member of the Graphics2 class with four available overloaded implementations (For details on the Graphics class, see 11, Creating Graphical Output) We start our discussion with the simplest of the overloaded functions, which accepts a pair of (x,y) single-precision floating-point values for the text location The simplest version of the DrawString method, with ve parameters, is de ned as follows:
Print Standard 2 Of 5 In VS .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Code 2 of 5 image in .NET applications.
public void DrawString( string str, Font font, Brush brText, Single x, Single y);
EAN-13 Supplement 5 Reader In VS .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
The rst parameter, str, identi es the string to draw While automatic word wrap is not supported, a carriage return within a string draws multiple lines (In C#, insert a new line with the \n character)
ECC200 Generation In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications.
2 SystemDrawingGraphics
Create USS Code 39 In .NET Framework
Using Barcode maker for ASP.NET Control to generate, create Code 39 image in ASP.NET applications.
Text and Fonts
EAN-13 Supplement 5 Generation In .NET
Using Barcode creation for ASP.NET Control to generate, create EAN-13 image in ASP.NET applications.
The second parameter, font, is the font used for drawing the characters This could be the default font of a control (the Font property) or a font that you create The third parameter, brText, identi es the brush for drawing text foreground pixels; in NET programming, background pixels are always untouched3 in text drawing The fourth and fth parameters, x and y, indicate the text-drawing locationThislocationistheupper-leftcorneroftherectanglethatboundsthetext These coordinates are single-precision oating-point values, which is different from the integer coordinates used to draw raster and vector graphics
Encoding Code 128B In C#.NET
Using Barcode generation for .NET framework Control to generate, create Code 128A image in Visual Studio .NET applications.
1213 A Sample Program: SimpleDrawString Our rst sample program shows the simplest way to draw in a form Figure 121 shows the program s output This program uses the form s default font to draw a character string using the system s default window text color
Code 128A Encoder In VB.NET
Using Barcode maker for .NET Control to generate, create Code 128 image in VS .NET applications.
Figure 121: Output of the SimpleDrawString Program
Decoding USS Code 128 In VS .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
As we discussed in 11, the Paint event plays a key role in any control with graphical output Listing 121 shows the Paint event handler method for our simple text-drawing sample This method is called whenever the window s client area needs drawing
Data Matrix 2d Barcode Maker In Java
Using Barcode creation for Java Control to generate, create ECC200 image in Java applications.
Listing 121: Fragment from SimpleDrawStringcs Showing the Paint Event Handler
private void formMain_Paint(object sender, PaintEventArgs e) { Single xDraw = 10; Single yDraw = 10;
3 Win32 programmers may recall that the BackgroundColor attribute in a device context allows a text-drawing operation to also affect the background pixels
122 Font Selection
Brush brText = new SolidBrush(SystemColorsWindowText); eGraphicsDrawString("Simple Draw String", Font, brText, xDraw, yDraw); // Highlight origin int x = (int)xDraw; int y = (int)yDraw; Pen penBlack = new Pen(ColorBlack); eGraphicsDrawLine(penBlack, x, y, x-8, y); eGraphicsDrawLine(penBlack, x, y, x, y-8); }
In this sample, the text is drawn at (10,10), the coordinates assigned to the xDraw and yDraw variables The second parameter specifies the font to use; in this code, we use the Font property from the formMain form In later examples, we show several ways to provide total control over the appearance of text Our program calls the DrawLine method to draw two lines one horizontal and one vertical that intersect at (10,10) The intersection of those two lines shows the default text alignment, at the upper-left corner of the drawn text The NET Compact Framework supports other alignments, as we show later in this chapter This capability lets you do things such as centering a string over a column of data or centering a text label within a graphical image This chapter s TextAlign sample shows all nine possible alignments
122 Font Selection
We now turn our attention to the subject of fonts Font selection is the primary way to control text appearance Windows CE supports two basic font methods: bitmap fonts and TrueType (also known as OpenType, for PostScript compatibility) fonts A given platform can support one but not both of these font types The decision about which to support is made by the platform creator and cannot be changed by an application program Some platforms support bitmap fonts to minimize the size of the operating system image The bene ts of supporting TrueType fonts include the ability to draw scalable fonts and the ability to draw rotated text What font support is found on Windows mobile devices Windows Mobile Professional (Pocket PC) supports TrueType fonts, and has these built in: Tahoma, Courier New and Bookdings Windows Mobile Standard