Resizing Controls at Runtime


With a couple of lines of code, you can give your end-users the ability to resize any control.

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 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 shown in the image above). The first thing, you'll need 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 only two events that are needed to be handled:
  • 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.

  • LButtonDown
  • 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 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) ;
    

Downloads

Download demo project - 9 Kb


Comments

  • memleaks

    Posted by Kerberos on 12/20/2012 02:33am

    You created memleaks by allocating RECT struct - dynamic allocation not needed at all...

    Reply
  • How to use CRectracker in CSrollView

    Posted by Legacy on 11/15/2003 12:00am

    Originally posted by: khurramMir

    Can AnyBody Help me How to Use CRectTracker in CsrollView
    

    Reply
  • Good one.

    Posted by Legacy on 08/01/2002 12:00am

    Originally posted by: Guru

    This one helped me..
    Thanks

    Reply
  • Can this Possible?.........

    Posted by Legacy on 01/15/2002 12:00am

    Originally posted by: Nagesh

    Hi
    I want to reieze my dialog depend upon the resolution used by the moniter in runtime. And even is it possible to resize dialog depend upon length and resolution of moniter.(i.e 14" Moniter with 1024*... resoliton /..etc)..
    waiting for reply
    Regds
    Nagesh

    Reply
  • Scroll within a view

    Posted by Legacy on 08/08/2001 12:00am

    Originally posted by: Jerome

    I have tried to use CRectTracker within a view to do something similar.
    What I would kie to achieve is that the tracker automatically scrolls the view when the mouse gets out of the boundaries.
    How could I achieve this ?
    Thanks,
    Jerome

    Reply
  • No RC

    Posted by Legacy on 07/20/2001 12:00am

    Originally posted by: Steve

    Unfortunately, you did not include the resouce files in the zip.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds