Click here to Skip to main content
11,706,455 members (51,556 online)
Click here to Skip to main content

A Multiple Selection based Control Tracker

, 19 Dec 2001 212.4K 5.9K 106
Rate this:
Please Sign up or sign in to vote.
A CRectTracker derived class that is able to do much more
<!-- Download Links -->

Introduction

Often in developing designer and CAD related applications, one needs the functionality of moving and resizing selected objects around. MFC run-time provides one class i.e. CRectTracker that does something like this, but it has several shortcomings, the most important one being that it does not have functionality for multiple object selection and resizing. This article introduces a class, CControlTracker that does much more than what CRectTracker does.

Control Tracker

It inherits from CRectTracker, and essentially has the same interface as CRectTracker. CControlTracker works closely with another class CControlRect, which inherits from CRectTracker and CWnd. This means that you can use CControlRect to create any control (by calling CWnd::Create) and add in the list of controls managed by CControlTracker. Then by simply overriding OnLButtonDown and OnSetCursor, you can get all the advanced functionality of moving/resizing controls:
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point) 
{	
	m_ControlTracker.Track( point , nFlags , true );
	CFrameWnd::OnLButtonDown(nFlags, point);
}

BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{	
	if( m_ControlTracker.SetCursor( nHitTest, message ) )
		return TRUE;	
	return CFrameWnd::OnSetCursor(pWnd, nHitTest, message);
}

Class Design

CControlRect inherits from CWnd, so you can pass CControlRect* wherever CWnd* is expected. It also inherits from CRectTracker. This is to allow every Control to "Track" itself. You can safely create all controls including buttons, static and ListBox controls and even activex controls, just as the way you would do with CWnd.
class CControlRect : public CRectTracker , public CWnd {
public:
// Controls enumerated in WindowType
	enum WindowType
	{
	enmScrollBar, enmStatic, enmButton, 
	enmEdit, enmListBox, enmComboBox
	};

// Can use enumerated type to create control
	BOOL Create( WindowType wndType, ... );
// Can also use class name to create control
	BOOL Create( LPCTSTR lpszClassName, ... );
// Tracks movement rectangle for control
	void Track( CWnd* pWnd, CPoint point, 
			BOOL bAllowInvert = FALSE, 
			CWnd* pWndClipTo = NULL );
// Sets bounding rectangle for control
	void SetRect( int x1, int y1, int x2, int y2 );
	void SetRect( const CRect & rect );
private:
// Tells weather the control is currently selected
	BOOL m_bSelected;
// Returns handle masks
	UINT GetHandleMask() const;
// Sets default attributes for CRectTracker
	void Initialize();
...
};
CControlTracker manages all objects of CControlRect class. It keeps an array of all controls added by a call to CControlTracker::Add. It also keeps an array of currently selected objects. Handles are only drawn for selected objects.
class CControlTracker : public CRectTracker
{
public:
// Creates the Control Tracker Object
	void Create( CWnd* pParentWnd );
// Adds the Control to the Tracker
	void Add( CControlRect* pObject );
// Starts tracking all the controls
	BOOL Track( const CPoint & point, UINT nFlags = 0, 
				BOOL bTrackRubberBand = TRUE );
// Sets the cursor on all the controls
	BOOL SetCursor( UINT nHitTest, UINT message );
// Tells weather the specified control is selected or not
	BOOL IsSelected( CControlRect* pObject ) const;
// Selects the specified control
// Returns TRUE if it was initially unselected else FALSE
	BOOL Select( CControlRect* pObject );
// DeSelects the specified control.
// Returns TRUE if it was initially selected else FALSE
	BOOL DeSelect( CControlRect* pObject );
// Toggles the state of the object 
// Returns TRUE if control was selected, otherwise returns FALSE
	BOOL Toggle( CControlRect* pObject );
// Draws all the controls
	void Draw( CDC* pDC ) const;
// DeSelects all the controls
// Returns the number of controls that were selected
	int DeSelectAll();
...
};

How to use it

Create instances of CControlTracker and CControlRect (controls) in your class. I have created controls in my application window class:
class CMainFrame : public CFrameWnd
{
	CControlTracker m_ControlTracker;
	CControlRect r1, r2;
	...
};
Override OnLButtonDown, OnSetCursor, OnDraw and OnCreate. In the OnCreate member of the Parent class, add the following code:
// Create ControlTracker
m_ControlTracker.Create( this );

// Create Controls
r1.Create( CControlRect::enmStatic, "Static Text Control", 
           this , SS_CENTERIMAGE | SS_BITMAP, CRect( 10,10,70,70 ) );
r2.Create( CControlRect::enmEdit, "Button Control",  
           this, WS_DISABLED,  CRect( 160,110,300,250 ) );

// Add controls in the tracking list of ControlTracker
	m_ControlTracker.Add( &r1 );
	m_ControlTracker.Add( &r2 );	

Future Work

In the near future, I would be working on incorporating LineTracker functionality into this tracker. You would be able to make a cool UML editor with quite ease using this class. Please do let me know if you want to suggest any improvements in the design or functionality.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

@deel@bbas
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionVery good codes, but where is the "Future Work"? Pin
qhbo24-Sep-14 17:20
memberqhbo24-Sep-14 17:20 
QuestionHow to select my picture and stretch my picture Pin
Member 1074064128-Apr-14 17:40
memberMember 1074064128-Apr-14 17:40 
Generalvery useful example! Pin
qusuo31-Jan-13 22:18
memberqusuo31-Jan-13 22:18 
QuestionAbout the license Pin
chris0050065-Apr-12 17:30
memberchris0050065-Apr-12 17:30 
Questiontrack custom control Pin
oldbishop16-Jan-12 10:52
memberoldbishop16-Jan-12 10:52 
GeneralMy vote of 5 Pin
zhangyongf4-Aug-10 21:30
memberzhangyongf4-Aug-10 21:30 
QuestionThe number of control rectangles seems to be limited to 5 Pin
JOHN117-Sep-09 8:22
memberJOHN117-Sep-09 8:22 
QuestionHow to delete a tracker control? Pin
imacess24-Sep-08 14:32
memberimacess24-Sep-08 14:32 
QuestionC# code for Multiple selection based Control Tracker ????? Pin
souravgantait22-Jul-08 16:31
membersouravgantait22-Jul-08 16:31 
GeneralCTrackerRect rotation Pin
duvaft7-Sep-04 21:43
memberduvaft7-Sep-04 21:43 
GeneralFreeHand Rotation Angle Pin
suyatchi19-Jun-04 0:11
membersuyatchi19-Jun-04 0:11 
QuestionHow can I implement a rectangle tracker over a bitmap ? Pin
Gem28-Mar-04 22:14
memberGem28-Mar-04 22:14 
GeneralWM_LBUTTONUP is not being captured Pin
Babak Asadi23-Apr-03 13:05
memberBabak Asadi23-Apr-03 13:05 
GeneralRe: WM_LBUTTONUP is not being captured Pin
Babak Asadi23-Apr-03 13:52
memberBabak Asadi23-Apr-03 13:52 
GeneralRe: WM_LBUTTONUP is not being captured Pin
sanskypotov17-Jun-03 23:16
membersanskypotov17-Jun-03 23:16 
QuestionHow to delete a control? Pin
zhang_dq20-Apr-03 21:20
memberzhang_dq20-Apr-03 21:20 
General[Message Deleted] Pin
Haresh Borse16-Apr-03 20:02
memberHaresh Borse16-Apr-03 20:02 
GeneralRe: How to extend this functionality for user defined controls Pin
HowardJones18-May-03 9:56
memberHowardJones18-May-03 9:56 
GeneralRe: How to extend this functionality for user defined controls Pin
Niko Tanghe20-Nov-03 0:36
memberNiko Tanghe20-Nov-03 0:36 
QuestionHow do I set the font for a static text control? Pin
Babak Asadi16-Apr-03 15:15
memberBabak Asadi16-Apr-03 15:15 
AnswerRe: How do I set the font for a static text control? Pin
Nitron16-Apr-03 15:54
memberNitron16-Apr-03 15:54 
GeneralRe: How do I set the font for a static text control? Pin
Babak Asadi16-Apr-03 19:13
memberBabak Asadi16-Apr-03 19:13 
GeneralCRectTracker::GetHandleMask Pin
newbee31-Jan-03 7:43
membernewbee31-Jan-03 7:43 
GeneralRe: CRectTracker::GetHandleMask Pin
[ELNINO]8-Sep-03 5:27
member[ELNINO]8-Sep-03 5:27 
GeneralRe: CRectTracker::GetHandleMask Pin
newbee8-Sep-03 7:04
membernewbee8-Sep-03 7:04 
GeneralCircular selection tool Pin
nitashr31-Jan-03 3:41
membernitashr31-Jan-03 3:41 
QuestionHow let it support coppy&paste? Pin
dxhdxh15-Dec-02 16:33
memberdxhdxh15-Dec-02 16:33 
GeneralActiveX control no move and resize Pin
billhao30-Nov-02 7:17
memberbillhao30-Nov-02 7:17 
GeneralRectangle not showing Pin
Andrew Bleakley28-Nov-02 18:28
memberAndrew Bleakley28-Nov-02 18:28 
GeneralRe: Rectangle not showing Pin
Andrew Bleakley28-Nov-02 18:31
memberAndrew Bleakley28-Nov-02 18:31 
GeneralCursors are not displayed when Use MFC in a Static Library Pin
lolont22-Oct-02 0:08
memberlolont22-Oct-02 0:08 
GeneralRe: Cursors are not displayed when Use MFC in a Static Library Pin
Andrea Serra13-Jan-03 20:39
memberAndrea Serra13-Jan-03 20:39 
GeneralCScrollView Pin
Alex Linde17-Sep-02 1:36
memberAlex Linde17-Sep-02 1:36 
GeneralRe: CScrollView Pin
Hugo galindo17-Oct-02 8:01
sussHugo galindo17-Oct-02 8:01 
GeneralControl Tracker Question Pin
Anonymous5-Sep-02 4:40
sussAnonymous5-Sep-02 4:40 
General<FONT COLOR="#FF0000">CRectTracker hidden when controls overlap </font> Pin
sanskypotov7-Jul-02 22:40
membersanskypotov7-Jul-02 22:40 
GeneralRe: CRectTracker hidden when controls overlap Pin
Arksoft3-May-07 8:31
memberArksoft3-May-07 8:31 
GeneralUsing CRectTracker in VB Pin
RamanSingla5-Jul-02 23:48
memberRamanSingla5-Jul-02 23:48 
GeneralRe: Using CRectTracker in VB Pin
Norm Almond6-Jul-02 0:02
memberNorm Almond6-Jul-02 0:02 
GeneralActivity Form Designer help me please Pin
jawad ayub baig12-May-02 21:12
memberjawad ayub baig12-May-02 21:12 
GeneralActiveX Pin
HallerbachJ24-Apr-02 8:35
memberHallerbachJ24-Apr-02 8:35 
GeneralRe: ActiveX Pin
humayun faiz abbas13-May-02 19:59
memberhumayun faiz abbas13-May-02 19:59 
GeneralRe: ActiveX Pin
billhao30-Nov-02 16:36
memberbillhao30-Nov-02 16:36 
GeneralRe: ActiveX Pin
mjpetersonsc10-Apr-03 10:54
membermjpetersonsc10-Apr-03 10:54 
QuestionHow about , I want to Move ONLY and Not Resize Pin
sanskypotov17-Apr-02 22:50
membersanskypotov17-Apr-02 22:50 
AnswerRe: How about , I want to Move ONLY and Not Resize Pin
Anonymous17-Apr-02 23:43
memberAnonymous17-Apr-02 23:43 
GeneralZooming the control Pin
p.virgili8-Feb-02 0:40
memberp.virgili8-Feb-02 0:40 
GeneralCeate new CControlRect Dynamically !! Pin
chunghansu7-Feb-02 1:46
memberchunghansu7-Feb-02 1:46 
GeneralRe: Ceate new CControlRect Dynamically !! Pin
@deel@bbas7-Feb-02 2:33
member@deel@bbas7-Feb-02 2:33 
Generalthanks ^^ Pin
chunghansu7-Feb-02 14:31
memberchunghansu7-Feb-02 14:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 20 Dec 2001
Article Copyright 2001 by @deel@bbas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid