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.

simple Text example ❘ OC27<br />

draWing TeXT<br />

We have chosen to cover the very important topic of displaying text late in this chapter because drawing text<br />

to the screen is (in general) more complex than drawing simple graphics. Although displaying a line or two<br />

of text when you don’t care about the appearance is extremely easy (it takes one single call to the Graphics.<br />

DrawString() method), if you are trying to display a document that has a fair amount of text in it, then<br />

you will rapidly find that things become a lot more complex. This is for two reasons:<br />

➤<br />

➤<br />

If you are concerned about getting the appearance just right, then you must underst<strong>and</strong> fonts.<br />

Whereas shape drawing requires brushes <strong>and</strong> pens as helper objects, the process of drawing text<br />

requires fonts as helper objects. Moreover, underst<strong>and</strong>ing fonts is not a trivial undertaking.<br />

Text needs to be very carefully laid out in the window. Users generally expect words to follow<br />

naturally from one word to another <strong>and</strong> to be lined up with clear spaces in between. Doing that<br />

is harder than you might think. For starters, you do not usually know in advance how much<br />

space on the screen a word is going to take up. That has to be calculated (using the Graphics.<br />

MeasureString() method). In addition, the space a word occupies on the screen affects where in the<br />

document every subsequent word is placed. If your application does any line wrapping, then it will<br />

need to assess word sizes carefully before deciding where to place the line break. The next time you<br />

run Microsoft Word, look carefully at the way Word is continually repositioning text as you do your<br />

work; there is a lot of complex processing going on there. Chances are that any GDI+ application<br />

you work on will not be nearly as complex as Word. However, if you need to display any text, many of<br />

the same considerations apply.<br />

In short, high-quality text processing is tricky to get right. However, putting a line of text on the screen,<br />

assuming that you know the font <strong>and</strong> where you want it to go, is actually very simple. Therefore, the next<br />

section presents a quick example that shows you how to display some text, followed by a short review<br />

of the principles of fonts <strong>and</strong> font families <strong>and</strong> a more realistic (<strong>and</strong> involved) text-processing example,<br />

CapsEditor.<br />

simPle TeXT eXamPle<br />

This example, DisplayText, is your usual Windows Forms effort. This time you override OnPaint() <strong>and</strong><br />

add member fields as follows:<br />

private readonly Brush blackBrush = Brushes.Black;<br />

private readonly Brush blueBrush = Brushes.Blue;<br />

private readonly Font haettenschweilerFont = new Font("Haettenschweiler", 12);<br />

private readonly Font boldTimesFont = new Font("Times New Roman", 10,<br />

FontStyle.Bold);<br />

private readonly Font italicCourierFont = new Font("Courier", 11,<br />

FontStyle.Italic | FontStyle.Underline);<br />

protected override void OnPaint(PaintEventArgs e)<br />

{<br />

base.OnPaint(e);<br />

Graphics dc = e.Graphics;<br />

dc.DrawString("This is a groovy string", haettenschweilerFont, blackBrush,<br />

10, 10);<br />

dc.DrawString("This is a groovy string " +<br />

"with some very long text that will never fit in the box",<br />

boldTimesFont, blueBrush,<br />

new Rectangle(new Point(10, 40), new Size(100, 40)));<br />

dc.DrawString("This is a groovy string", italicCourierFont, blackBrush,<br />

new Point(10, 100));<br />

}<br />

code download DisplayText.sln<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!