15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

OC28 ❘ ChaPTer 48 GrAphics with Gdi+<br />

Figure 48 - 15 shows the result of running this example.<br />

The example demonstrates the use of the Graphics.DrawString()<br />

method to draw items of text. The method DrawString() comes in<br />

a number of overloads, three of which are demonstrated here. The<br />

different overloads require parameters that indicate the text to be<br />

displayed, the font that the string should be drawn in, <strong>and</strong> the brush<br />

that should be used to construct the various lines <strong>and</strong> curves that make<br />

up each character of text. A few alternatives exist for the remaining<br />

parameters. In general, however, it is possible to specify either a Point<br />

(or equivalently, two numbers) or a Rectangle .<br />

If you specify a Point , then the text will start with its top - left corner<br />

at that Point <strong>and</strong> simply stretch out to the right. If you specify a<br />

Rectangle , then the Graphics instance will lay out the string inside<br />

figure 48-15<br />

that rectangle. If the text does not fi t within the boundaries of the<br />

rectangle, it will be cut off (see the fourth line of text in Figure 48 - 15). Passing a rectangle to DrawString()<br />

means that the drawing process will take longer because DrawString() will need to fi gure out where to put<br />

line breaks, but the result may look nicer — provided the string fi ts in the rectangle!<br />

This example also shows a few ways to construct fonts. You always need to include the name of the font <strong>and</strong><br />

its size (height). You can also optionally pass in various styles that modify how the text is to be drawn (bold,<br />

underline, <strong>and</strong> so on).<br />

fonTs <strong>and</strong> fonT families<br />

A font describes exactly how each letter should be displayed. Selection of the appropriate font <strong>and</strong> providing<br />

a reasonable variety of fonts within a document are important factors in improving readability.<br />

Most people, if asked to name a font, might mention Arial or Times New Roman (if they are Windows<br />

users) or Times or Helvetica (if they are Mac OS users). In fact, these are not fonts at all — they are font<br />

families. The font family tells you, in generic terms, the visual style of the text <strong>and</strong> is a key factor in the<br />

overall appearance of your application. Most of us recognize the styles of the most common font families,<br />

even if we are not consciously aware of it.<br />

An actual font would be something like Arial 9 - point italic. In other words, the size <strong>and</strong> other modifi cations<br />

to the text are specifi ed as well as the font family. These modifi cations might include whether text is<br />

bold , italic, underlined, or displayed in small caps or as subscript<br />

; this is technically referred to as the style,<br />

although in some ways, the term is misleading because the visual appearance is determined as much by the<br />

font family.<br />

The size of the text is measured by specifying its height. The height is measured in points — a traditional<br />

unit that represents 1 / 72 of an inch (0.351 mm). So letters in a 10 - point font are roughly 1 / 7 of an inch,<br />

or 3.5 mm high. However, you will not get seven lines of 10 - point text into one inch of vertical screen or<br />

paper space, as you need to allow for the spacing between the lines as well.<br />

Strictly speaking, measuring the height is not quite as simple as that because there are<br />

several different heights that you must consider. For example, there is the height of tall<br />

letters such as the A or F (this is the measurement that we are referring to when we talk<br />

about the height), the additional height occupied by any accents on letters such as Å or<br />

Ñ (the internal leading), <strong>and</strong> the extra height below the baseline needed for the tails of<br />

letters such as y <strong>and</strong> g (the descent). However, for this chapter we will not worry about<br />

that. When you specify the font family <strong>and</strong> the main height, these subsidiary heights are<br />

determined automatically.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!