03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In theory, then, you don't need to code any new functions in order to do an applicationinitiated<br />

redraw. You can simply call DrawNow() so that your Draw() function is called in<br />

turn.<br />

Where to draw<br />

It's possible that only a part of your control will need to be drawn (or redrawn). To<br />

understand this, you need to distinguish between the four regions shown in Figure 11.6:<br />

Figure 11.6<br />

Your control is part of a window. The window server knows about the window and knows the<br />

regions of the window that are invalid – that is, the parts that need to be redrawn. Your<br />

Draw() function must draw the entire invalid region, but it must not draw outside the<br />

boundary of the control.<br />

The window server will clip drawing to the invalid region – which is clearly bounded, in turn,<br />

by the boundary of the window itself.<br />

Important<br />

But if your control doesn't occupy the entire window, you are<br />

responsible for ensuring that your redraw doesn't spill beyond the<br />

boundaries of the control.<br />

Often, this turns out to be not too onerous a responsibility: many controls such as buttons<br />

and the various sections of the Battleships application screen, draw rectangles, lines, and<br />

text are guaranteed to be inside the control's boundary in any case.<br />

In the few cases in which this doesn't happen, you can issue a SetClippingRect() call to<br />

the graphics context that ensures that future drawing is clipped to the control's rectangle.<br />

Here's an example, from the drawing example developed in <strong>Chapter</strong> 15:<br />

aGc.SetClippingRect(aDeviceRect);<br />

aGc.SetPenColor(KRgbDarkGray);<br />

aGc.DrawRect(surround);<br />

This is necessary because surround could have been bigger than aDeviceRect, which is<br />

the region of the control that this code is allowed to draw into. You can cancel this later, if<br />

you wish, with CancelClippingRect(), but since CGraphicsContext::Reset() does<br />

this anyway and Reset() is called prior to each control's Draw(), you don't need to do this<br />

explicitly from a control.

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

Saved successfully!

Ooh no, something went wrong!