11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 11. Sample: Simple Paint<br />

Temporary rectangles are drawn by calling function CDC::DrawFocusRect(…). Because this function<br />

uses XOR drawing mode, it is easy to erase the previous rectangle by simply calling the function twice.<br />

When the left button is released, we erase the previous temporary rectangle if necessary, update the<br />

tracker rectangle, and call function CWnd::InValidate() to let the tracker be updated (along with the client<br />

window).<br />

Because we must keep track of mouse cursor position after its left button is pressed down, the window<br />

capture must be set when a new tracker is being created. Since we share the code implemented for dot and<br />

line drawing here, there is no need to add extra code to set window capture for the client window here.<br />

If Mouse Clicking Hits the Tracker<br />

If mouse clicking hits the tracker (any of the resize buttons, or the middle of the tracker), we must<br />

implement tracking to let the user resize or move the existing tracker. This can be easily implemented by<br />

calling function CRectTracker::Track():<br />

……<br />

……<br />

else<br />

{<br />

::ReleaseCapture();<br />

m_trackerSel.Track(this, point);<br />

Invalidate();<br />

}<br />

Because the window capture is set when a new tracker is being created (also, when a dot or a line is<br />

being drawn), we must first release the capture before trackering mouse movement (Otherwise the tracker<br />

will not be able to receive messages related to mouse moving events). There is no need for us to handle<br />

WM_MOUSEMOVE and WM_LBUTTONUP messages here because after function CRectTracker::Track() is called,<br />

mouse moving events will all be routed to the tracker. After this function exits, variable<br />

CRectTracker::m_rect will be automatically updated to represent the new size and position of the tracker.<br />

So after calling this function, we can update the client window directly to redraw the tracker.<br />

11.5 Moving the Selected Image<br />

Sample 11.5\GDI is based on sample 11.4\GDI. It is implemented with a new feature: when a portion<br />

of the bitmap is selected by the tracker, the user can move the selected image by dragging the tracker to<br />

another place; if the user resizes the tracker, the selected image will also be stretched.<br />

Normalizing Tracker<br />

Since the tracker rectangle is recorded in the zoomed image’s coordinate system, we must first convert<br />

it back to the original bitmap’s own coordinate system (the image with 1:1 ratio) in order to find out which<br />

part of the image is being selected. In the sample, function CGDIView::NormalizeTrackerRect(…) is added<br />

for this purpose. In this function, the current image ratio is retrieved from the document, and the four points<br />

of the tracker rectangle is divided by this ratio. The tracker can be created in two different ways. For<br />

example, the user may click and hold the left mouse button and drag it right-and-downward; also, the<br />

mouse may be dragged up-and-leftward. For the first situation, a normal rectangle will be formed, in which<br />

case member CRectTracker.m_rect.left is always less than member CRectTracker.m_rect.right, and<br />

CRectTracker.m_rect.top is less than CRectTracker::m_rect.bottom. However, in the second situation,<br />

CRectTracker.m_rect.left and CRectTracker.m_rect.top are all grater than their corresponding<br />

variables. So before using variable CRectTracker::m_rect, we must normalize the rectangle.<br />

We can call function CRect::NormalizeRect() to normalize a rectangle implemented by class CRect.<br />

In function CGDIView::NormailizeTrackerRect(…), before the four points of the tracker rectangle are<br />

divided by the ratio, this function is called to first normalize the rectangle.<br />

349

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

Saved successfully!

Ooh no, something went wrong!