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 ❘ OC37<br />

longest. For both height <strong>and</strong> width, you will also want to make an allowance for a small margin around the<br />

displayed document to make the application look more attractive.<br />

The following is the method that calculates the document size:<br />

private void CalculateDocumentSize()<br />

{<br />

if (!documentHasData)<br />

{<br />

documentSize = new Size(100, 200);<br />

}<br />

else<br />

{<br />

documentSize.Height = (int)(nLines*lineHeight) + 2*(int)margin;<br />

uint maxLineLength = 0;<br />

foreach (TextLineInformation nextWord in documentLines)<br />

{<br />

uint tempLineLength = nextWord.Width;<br />

if (tempLineLength > maxLineLength)<br />

{<br />

maxLineLength = tempLineLength;<br />

}<br />

}<br />

maxLineLength += 2*margin;<br />

documentSize.Width = (int)maxLineLength;<br />

}<br />

AutoScrollMinSize = documentSize;<br />

}<br />

code download CapsEditor.sln<br />

This method first checks whether there is any data to be displayed. If there is not, then you cheat a bit <strong>and</strong><br />

use a hard-coded document size, which is big enough to display the big red warning.<br />

If you had wanted to really do it properly, you would have used MeasureString() to check how big that<br />

warning actually is.<br />

When you have worked out the document size, you tell the Form instance what the size is by setting the<br />

Form.AutoScrollMinSize property. When you do this, something interesting happens behind the scenes.<br />

In the process of setting this property, the client area is invalidated <strong>and</strong> a Paint event is raised, for the<br />

very sensible reason that changing the size of the document means scrollbars will need to be added or<br />

modified <strong>and</strong> the entire client area will almost certainly be repainted. Why is that interesting If you look<br />

back at the code for LoadFile(), you will realize that the call to Invalidate() in that method is actually<br />

redundant. The client area will be invalidated anyway when you set the document size. The explicit call to<br />

Invalidate() was left in the LoadFile() implementation to illustrate how you should normally do things.<br />

In fact, in this case, calling Invalidate() again will only needlessly request a duplicate Paint event.<br />

However, this in turn illustrates how Invalidate() gives Windows the chance to optimize performance.<br />

The second Paint event will not, in fact, get raised: Windows will see that there is a Paint event already<br />

sitting in the queue <strong>and</strong> will compare the requested invalidated regions to see if it needs to do anything to<br />

merge them. In this case, both Paint events will specify the entire client area, so nothing needs to be done,<br />

<strong>and</strong> Windows will quietly drop the second Paint request. Of course, going through that process will take<br />

up a little bit of processor time, but it will be a negligible amount of time compared to how long it takes to<br />

actually do some painting.<br />

onPaint()<br />

Now that you have seen how CapsEditor loads the file, it’s time to look at how the painting is done:<br />

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

{<br />

base.OnPaint(e);<br />

Graphics dc = e.Graphics;<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!