Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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

Share

About the Author

Amit Nabarro

United States United States
No Biography provided

Comments and Discussions

 
GeneralActivex resize PinmemberRondina 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
 

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 | Mobile
Web02 | 2.8.140922.1 | Last Updated 1 Dec 2000
Article Copyright 2000 by Amit Nabarro
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid