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.

editing a Text Document: The Capseditor example ❘ OC31<br />

constructor for Font — there are many more that allow additional options to be specified. The constructor<br />

takes two parameters, the name of the family <strong>and</strong> the size of the font:<br />

Font f = new Font(family.Name, 10);<br />

This constructor builds a font that has the regular style. To be on the safe side, however, you first check that<br />

this style is available for each font family before attempting to display anything using that font. This is done<br />

using the FontFamily.IsStyleAvailable() method. This check is important because not all fonts are<br />

available in all styles:<br />

if (family.IsStyleAvailable(FontStyle.Regular))<br />

FontFamily.IsStyleAvailable() takes one parameter, a FontStyle enumeration. This enumeration<br />

contains a number of flags that might be combined with the bitwise OR operator. The possible flags are<br />

Bold, Italic, Regular, Strikeout, <strong>and</strong> Underline.<br />

Finally, note that you use a property of the Font class, Height, which returns the height needed to display<br />

text of that font, to work out the line spacing:<br />

Font f = new Font(family.Name, 10);<br />

Point topLeftCorner = new Point(margin, verticalCoordinate);<br />

verticalCoordinate += f.Height;<br />

Again, to keep things simple, this version of OnPaint() reveals some bad programming practices.<br />

For example, you have not bothered to check what area of the document actually needs drawing —<br />

you just tried to display everything. Also, instantiating a Font is, as remarked earlier, a computationally<br />

intensive process, so you really ought to save the fonts rather than instantiating new copies every time<br />

OnPaint() is called. Because of the way the code has been designed, you might note that this example<br />

actually takes a noticeable amount of time to paint itself. To try to conserve memory <strong>and</strong> help the garbage<br />

collector, you do, however, call Dispose() on each font instance after you have finished with it. If you did<br />

not, after 10 or 20 paint operations, there would be a lot of wasted memory storing fonts that are no<br />

longer needed.<br />

ediTing a TeXT doCumenT: The CaPsediTor eXamPle<br />

You now come to the extended example in this chapter. The CapsEditor example is designed to<br />

demonstrate how the principles of drawing that you have learned so far have to be applied in a more<br />

realistic context. The CapsEditor example does not require any new material, apart from responding to<br />

user input via the mouse, but it shows how to manage the drawing of text so that the application maintains<br />

performance while ensuring that the contents of the client area of the main window are always kept<br />

up-to-date.<br />

The CapsEditor program allows the user to read in a text file, which is then displayed line by line in the<br />

client area. If the user double-clicks any line, then that line will be changed to all uppercase. That is literally<br />

all the example does. Even with this limited set of features, you will find that the work involved in making<br />

sure everything is displayed in the right place while considering performance issues is quite complex. In<br />

particular, you have a new element here: the contents of the document can change — either when the user<br />

selects the menu option to read a new file, or when she double-clicks to capitalize a line. In the first case,<br />

you need to update the document size so the scrollbars still work correctly, <strong>and</strong> you have to redisplay<br />

everything. In the second case, you need to check carefully whether the document size has changed, <strong>and</strong><br />

what text needs to be redisplayed.<br />

This section starts by reviewing the appearance of CapsEditor. When the application is first run, it has no<br />

document loaded <strong>and</strong> resembles Figure 48-17.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!