11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11. Sample: Simple Paint<br />

customized (The cursor is over tracker’s region). In this case we can exit and return a TRUE value.<br />

Otherwise we must call function CWnd::OnSetCursor(…) to let the cursor’s shape be set to the default one.<br />

New Tool<br />

In the new sample application, a new tool “Rectangular Selection” is implemented in tool bar<br />

IDR_DRAWTOOLBAR (Figure 11-5). If it is selected as the current tool, the user can drag the mouse to create a<br />

tracker over the image, resize or move it to change the selection. The cursor will be automatically changed<br />

if the mouse cursor is within the tracker’s region.<br />

“Rectangular<br />

Selection” button on<br />

the tool bar<br />

Figure 11-5. Tool “Rectangular Selection”<br />

First, a new command ID_BUTTON_RECSEL is added to IDR_DRAWTOOL tool bar. Each time a new tool<br />

command is added to the tool bar, we must make sure that the IDs of all the commands contained in the<br />

tool bar are consecutive. Otherwise the macros ON_COMMAND_RANGE and ON_UPDATE_COMMAND_UI_RANGE will<br />

not work correctly. In the sample, two macros TOOL_HEAD_ID and TOOL_TAIL_ID are defined, and they<br />

represent the first and last IDs of the commands contained in the drawing tool bar. We use the above two<br />

macros to do the message mapping. By doing this, if we add a new tool next time, all we need to do is<br />

redefining the macros.<br />

In class CGDIView, a CRectTracker type variable m_trackerSel is declared to implement the tracker.<br />

The tracker’s styles are initialized in the constructor as follows:<br />

……<br />

……<br />

m_trackerSel.m_nStyle=<br />

(<br />

CRectTracker::dottedLine | CRectTracker::resizeOutside<br />

);<br />

The tracker’s border is formed by dotted line and the resize buttons are located outside the rectangle.<br />

The tracker is drawn in function CGDIView::OnDraw(…) if the tracker rectangle is not empty:<br />

……<br />

……<br />

CPoint ptScroll;<br />

ptScroll=GetScrollPosition();<br />

m_trackerSel.m_rect.OffsetRect(-ptScroll);<br />

m_trackerSel.Draw(pDC);<br />

m_trackerSel.m_rect.OffsetRect(ptScroll);<br />

As we will see, the tracker’s size and position will be recorded in the zoomed bitmap image’s<br />

coordinate system. This is for the convenience of coordinate conversion. Since the DC will draw the tracker<br />

in client window’s coordinate system, we must add some offset to the tracker rectangle before it is drawn.<br />

Also we need to resume rectangle’s original state after the drawing is completed.<br />

Function CGDIView::OnSetCursor(…) is modified as follows so that the cursor will be automatically<br />

changed if it is over the tracker region:<br />

……<br />

if(m_bmpDraw.GetSafeHandle() != NULL)<br />

{<br />

if<br />

(<br />

!(<br />

347

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

Saved successfully!

Ooh no, something went wrong!