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

Resizing Controls at run time

, 30 Nov 2000
Rate this:
Please Sign up or sign in to vote.
A method to allow the user to visually resize and position any control at run time.

Sample Image - resize_at_runtime.gif


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));
m_tracker = new CRectTracker(rect,CRectTracker::dottedLine  | 
                CRectTracker::resizeOutside | 
                CRectTracker::hatchedBorder );

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);
    CDC* pDC = GetDC();

    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!


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
No Biography provided

Comments and Discussions

Questiontracker is coveraged by the sibling object Pinmemberhandsome0314-Jun-12 18:56 
GeneralActivex resize PinmemberRondina B20-May-04 5:57 
GeneralActivex resize PinsussAnonymous20-May-04 5:45 
Questionhow to use from c# application Pinsussjbmeeh13-Feb-04 11:33 
QuestionHow to save the new dialog layout back to a resource? PinmemberDJM5-Aug-03 2:25 
AnswerRe: How to save the new dialog layout back to a resource? PinmemberDavid Pritchard17-Nov-04 4:08 
GeneralCursor goes away when linked with Static Library Pinmembercdsmith29-Apr-03 7: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?
Craig Smith
GeneralRe: Cursor goes away when linked with Static Library Pinmemberlesnikowski9-Jan-04 3:20 
GeneralRe: Cursor goes away when linked with Static Library Pinmembertohichoi19-Sep-05 21:57 
GeneralRe: Cursor goes away when linked with Static Library PinmemberM A V11-Jul-08 7:56 
AnswerRe: Cursor goes away when linked with Static Library PinmemberAnthony Dunk1-Jul-09 20:34 
GeneralI have a problem PinmemberBoscoW21-Feb-03 23:38 
GeneralGreat job PinmemberSynetech18-Feb-03 9:30 
GeneralNE?YO BLAD! PinmemberBooooo18-Nov-02 7:12 
General<FONT COLOR="#FF0000">CRectTracker hidden when controls overlap </font> Pinmembersanskypotov7-Jul-02 23:05 
GeneralProblem PinmemberAnonymous15-Jun-01 1:03 
GeneralRe: Problem PinmemberAnonymous15-Oct-01 22:38 
GeneralRe: Problem PinmemberJ Cardinal9-Apr-02 15:10 

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
Web03 | 2.8.150302.1 | Last Updated 1 Dec 2000
Article Copyright 2000 by Amit Nabarro
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid