Click here to Skip to main content
Click here to Skip to main content

Resizing Controls at run time

By , 30 Nov 2000
 

Sample Image - resize_at_runtime.gif

Introduction

Suppose you wanted to give the user the ability to modify the size and position on a certain control? This example shows how to implement resizing controls on a dialog box as it is done when drawing controls on a dialog template or using Visual Basic at design time.

In order to accomplish this, we can use the class CRectTracker to manage all the drawing and resizing of a rectangular frame which also has (optional) 6 resize handlers (as in the image above). First thing to do is to invoke a CRectTracker and specify given coordinates:

LPRECT rect = new RECT;
CWnd* wnd = (CWnd*)(GetDlgItem(IDC_EDIT1));
wnd->GetWindowRect(rect);
ScreenToClient(rect);
m_tracker = new CRectTracker(rect,CRectTracker::dottedLine  | 
                CRectTracker::resizeOutside | 
                CRectTracker::hatchedBorder );
m_tracker->Draw(pDC);

There are two events that are needed to be taken care of:

  1. SetCursor
    if (pWnd == this && m_tracker->SetCursor(this, nHitTest))
        return TRUE;

    This is done in order to draw the correct mouse cursors when floating the mouse pointer over the rectangle.

  2. OnLButtonDown
    m_tracker->Track(this, point, TRUE);
    Invalidate(FALSE);
    CDC* pDC = GetDC();
    m_tracker->Draw(pDC);

    This will take care of the drawing of the rectangle with resizing it.

Once you have finished, all you have to do is draw the control with the new rectangle coordinates:

LPRECT rect = new RECT;
CWnd* wnd = (CWnd*)(GetDlgItem(IDC_EDIT1));
rect = LPRECT(m_tracker->m_rect); 
wnd->MoveWindow(rect,TRUE) ;

That's it!

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

About the Author

Amit Nabarro
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiontracker is coveraged by the sibling objectmemberhandsome0314 Jun '12 - 17:56 
When 2 controls overlap each other and if they are selected
( in other words they are showing the tracker) then the
crecttracker is hidden in the overlapping place.
Please can please tell me the solution on how to overcome this.
GeneralActivex resizememberRondina B20 May '04 - 4:57 
Good job well done.
I'd like to use it on Activex control (OCX).
My idea was to do final user can move the control at runtime
as the same way as I do on Visual basic at design time.
 
I make a function:
void CMyClassCtrl::Tracker(BOOL bTracker)
{
if (bTracker){
if (m_tracker){
delete m_tracker;
m_tracker = NULL;
}
CDC* pDC=(CDC*)GetDC();
 
LPRECT rect = new RECT;
CRect rect;
GetRectInContainer(&rect) ;
 
m_tracker = new CRectTracker(rect,CRectTracker::dottedLine | CRectTracker::resizeOutside | CRectTracker::hatchedBorder );
m_tracker->Draw(pDC) ;
delete rect;
rect = NULL;
SetModifiedFlag();
}
else {
if (m_tracker){
delete m_tracker;
m_tracker = NULL;
Invalidate(TRUE);
}
}
}
This code fail because at run time when I call this
funcion only a part of rect is showed, not all around the
control as I wanted.
The function GetRectInContainer(&rect) does't seem to retun
right rect of client area. Do I have to transform it on other coordinate?
 
What is wrong, can you help me ? thank you
 

GeneralActivex resizesussAnonymous20 May '04 - 4:45 
sd
Questionhow to use from c# applicationsussjbmeeh13 Feb '04 - 10:33 
I have been researching how to resize controls at runtime from a C# application and there is no easy way. C# does not have the equivalent of the CRectTracker class. It appears that my best option is to call your routine from a C# application. Any suggestions on the best way to link the events. Is there the equivalent of the OnSetCursor event in C#? Any help would be appreciated.
QuestionHow to save the new dialog layout back to a resource?memberDJM5 Aug '03 - 1:25 
It's slightly off topic but:
How can you save the new dialog layout back to a resource file?
 
Any help would be very welcome...
AnswerRe: How to save the new dialog layout back to a resource?memberDavid Pritchard17 Nov '04 - 3:08 
That's actually pretty complicated. It would be another article, or maybe a book. It can certainly be done though.
GeneralCursor goes away when linked with Static Librarymembercdsmith29 Apr '03 - 6:34 
I am having a problem with CRectTracker. When I link with Shared DLL it works fine, when I link with Static Library the cursors (resize and move) do not show up when the mouse is over the tracking rect. I was going crazy thinking I was doing something wrong, but this code has exactly the same problem.
 
Anybody know why the cursors do not show up when statically linked?
 
Thanks!
 
Craig Smith
GeneralRe: Cursor goes away when linked with Static Librarymemberlesnikowski9 Jan '04 - 2:20 
Add _AFXDLL in Project's configuration Properties/ "C/C++" / Preprocessor / Preprocessor Definitions.
 
In Code Generation / RunTime Library use "Multi-Threaded Dll" (/MD)
 

GeneralRe: Cursor goes away when linked with Static Librarymembertohichoi19 Sep '05 - 20:57 
If you can use dynamically linked MFC dll, just follow up Mr. lesnikowski's suggestion.
 
Otherwise, initialize cursor resource by your hand. For example, simple initialization would be
 

 
extern HCURSOR _afxCursors[10];
 
BOOL _InitCursor()
{
HINSTANCE hInst=NULL;
 
if (!_afxCursors[0])
_afxCursors[0] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZENWSE));
 
if (!_afxCursors[1])
_afxCursors[1] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZENESW));
 
if (!_afxCursors[2])
_afxCursors[2] = _afxCursors[0];
 
if (!_afxCursors[3])
_afxCursors[3] = _afxCursors[1];
 
if (!_afxCursors[4])
_afxCursors[4] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZENS));
 
if (!_afxCursors[5])
_afxCursors[5] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZEWE));
 
if (!_afxCursors[6])
_afxCursors[6] = _afxCursors[4];
 
if (!_afxCursors[7])
_afxCursors[7] = _afxCursors[5];
 
if (!_afxCursors[8])
_afxCursors[8] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZEALL));
 
if (!_afxCursors[9])
_afxCursors[9] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZEALL));
 
return TRUE;
}
 
BOOL CAdjustWBoxDialog::OnInitDialog()
{
...
m_tracker=new CRectTracker(rect, CRectTracker::solidLine|CRectTracker::resizeOutside);
_InitCursor();
...
}

 
Please note that _InitCursor() should be called after instantiation of m_tracker(see the MFC source trckrect.cpp).
 

GeneralRe: Cursor goes away when linked with Static LibrarymemberM A V11 Jul '08 - 6:56 
See this article http://support.microsoft.com/kb/208856[^]
 
CAUSE
When an application is linked to MFC by using the static library, the MFC resources are compiled into the executable of an application. The executable of the application will include the Afxres.rc file. To check this, click Resource Includes on the View menu. The Afxres.rc file has the resources that CRectTracker uses in the MFC source code.
 
These resources are only included when _AFX_NO_TRACKER_RESOURCES is not defined. The BLOCKS32 project has _AFX_NO_TRACKER_RESOURCES defined. Therefore, it does not put the resources that CRectTracker requires in the executable image of the application. Therefore, none of the cursors that are used by CRectTracker will show up when you build the sample by using MFC statically.
 

RESOLUTION
1. On the View menu, click Resource Includes.
2. Delete the following line:
 
#define _AFX_NO_TRACKER_RESOURCES
AnswerRe: Cursor goes away when linked with Static LibrarymemberAnthony Dunk1 Jul '09 - 19:34 
On VS2008, I found that the "Resource Includes" item is under the Edit menu.
 
The line in the .rc was as follows:
 
#define _AFX_NO_SPLITTER_RESOURCES
 
When I deleted this in my application (which is statically linked to MFC), the splitter cursors returned.
GeneralI have a problemmemberBoscoW21 Feb '03 - 22:38 
i have try to use this code in an other application
but when i start rezice at run and i go over the elment i see no mouse WTF | :WTF: OMG | :OMG: that isn't ver good Poke tongue | ;-P becuase you can't see if you are over the rezice rect or ocer the rest Confused | :confused: Confused | :confused: Confused | :confused:
 

soooo please help me

GeneralGreat jobmemberSynetech18 Feb '03 - 8:30 
Well I for one would like to compliment you. This is a great idea and the demo project works perfectly.
 
I am going to try using the code you've included when I get home.
 
As for the comlaints below, the one about deleting a NULL crect is valid however that is probably just a cut-n-paste error that slipped through. As for the overlapping tracker problem I'd just suggest to resize one control at a time.
 
Again good job and I'll try it tonight. Smile | :)
 
--
FireBox @ Synetech

GeneralNE?YO BLAD!memberBooooo18 Nov '02 - 6:12 
U GOT A FOOONE? N WHERES DAT ARTICLE WHERE DA MUSLIM GEEZA WAS GETIN GUNED?Laugh | :laugh:
 
-------------
Oi BlAd U GoT a fOnE oN U?
General<FONT COLOR="#FF0000">CRectTracker hidden when controls overlap </font>membersanskypotov7 Jul '02 - 22:05 
When 2 controls overlap each other and if they are selected
( in other words they are showing the tracker) then the
crecttracker is hidden in the overlapping place.
Please can please tell me the solution on how to overcome this.
 

 
John 3:16

For God so loved the world,

that he gave his only begotten Son ( Jesus Christ ) ,

that whosoever believeth in him should not perish, but have everlasting life.

GeneralProblemmemberAnonymous15 Jun '01 - 0:03 
Probably not a good idea, in CResizeDlg::OnLButtonDown:
 
rect = NULL;
delete rect;
rect = NULL;
GeneralRe: ProblemmemberAnonymous15 Oct '01 - 21:38 
.. although the code is perfectly legal, it indicates this guy obviously doesn't know what he's doing.
GeneralRe: ProblemmemberJ Cardinal9 Apr '02 - 14:10 
Sigh!
 

 
Ground Zero Tech-Works
http://www.ayanova.com

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 1 Dec 2000
Article Copyright 2000 by Amit Nabarro
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid