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   
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
Web02 | 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