Limit CRectTracker handles






4.72/5 (8 votes)
Jul 20, 2004
2 min read

47313

1978
Limit additional resize operations to a single handle.
Summary
I had a need to restrict MFC's CRectTracker
handles. Once the user selected an object and resized via any one handle of the tracker, I could not let them then grab a different handle. Via an override to CRectTracker
and a modification of the handle mask, this was accomplished.
This article discusses how to change the tracker handle mask. As a side issue, the demo may be referenced as an example of how to use the tracker's rubber band feature.
Background
CRectTracker
is placed in a mouse function, often, in OnMouseMove()
.
The tracker rectangle may be displayed with handles either inside or outside of the rectangle. Once the user grabs a handle with a clink and hold, CRectTracker
captures all further mouse move messages. No further points are reported to OnMouseMove()
until the user releases the tracker handle.
This class is very useful to rubber band a rectangle.
There is a handle mask to define which handles are displayed. It may be obtained via GetHandleMask()
.
- 0x00 0000 0000 is translate (no handles).
- 0x0F 0000 1111 is the 4 corners.
- 0x50 0101 0000 is the right and left sides.
- 0xA0 1010 0000 is the top and bottom.
Handle's returns are numbered by the four corners clockwise 0-3, then the four centers from the top center clockwise 4-7.
By manipulating this mask, one can disable handles. Thus to limit a resize in that direction. To access the mask, one must derive a new class from CRectTracker
. As my company is named WedgeSoft, I named my class CWS_RectTracker
.
CWS_RactTracker
is a very simple override, doing only one thing: modifying the handle mask.
The Sample
This sample code can be used to learn a bit about how to use CRectTracker
as well as see how to turn off any handle. All contents were researched through MSDN library CDs. To give the user something to resize, I create a poly line via a CWS_PolyLine
class. The class is basically useless. But I had fun creating it. Perhaps you can learn something about translation there.
In addition, there is a useful little function to display a bitmask.
What to look for
When you run the program, use tracker's rubber band to create a poly line. Then use tracker's rubber band to select that poly line. Then resize. Note that once resized, only the first handle selected may be used for further resize operations. A click within the bounds of the poly line allows the item to be translated. A double click anywhere turns off the resize tracker. (See under menu Help, HowTo.)
In OnMouseMove()
, a tracker rubber band is activated to select an existing poly line or insert a new poly line. In OnLButtonDown()
, a tracker hit test is performed to see if a tracker handle is selected. OnSetCursor()
sets the proper tracker cursor.