Class to Auto-Position Controls on Window Resize


Introduction

Have you ever wanted to resize a window but NOT have to deal with all of that annoying control-repositioning? What a hassle! Here is an easy-to-use class that should provide a good foundation for anyone wishing to easily reposition controls when a user resizes the parent window.

Usage

In order to use this class, simply follow these steps:
  1. Create a CControlPos member variable in your CDialog (used in example) or CView derived class.
  2. class CYourDialog : public CDialog 
    {
    ...
     CControlPos m_cControlPos;
    
  3. In the OnInitDialog (or InitialUpdate) function, set the object's parent window
  4. BOOL CYourDialog::OnInitDialog()
    ...
     m_cControlPos.SetParent(this);
    ...
    
  5. After the call to CControlPos::SetParent, Add the controls you want to resize.
  6. m_cControlPos.AddControl(IDOK, CP_MOVE_HORIZONTAL);
    m_cControlPos.AddControl(IDCANCEL, CP_MOVE_HORIZONTAL);
    
  7. Finally, on your WM_SIZE handler, call the object's MoveControls to specify runtime behaviour. move all of the controls. Controls can be moved horizontally and vertically and resized horizontally and vertically.
  8. void CYourDialog::OnSize(UINT nType, int cx, int cy) 
    {
     CDialog::OnSize(nType, cx, cy);
    	
     m_cControlPos.MoveControls();
    }
    

Downloads

Download source - 4 Kb
Download demo project - 17 Kb


Comments

  • Sr. Project Manager

    Posted by dvfds on 06/10/2014 09:31am

    Thanks for the CControlPos class. I'm interested in using it in a commercial product. Please let me know if this is acceptable or if you have any restrictions on it's use.

    Reply
  • One small modification...

    Posted by adinelt on 03/27/2006 02:26pm

    Thanks for the great class Paul. I did make one small change to it though. I found that when I was resizing a window, the controls would not always refresh properly. As an example, when a button would redraw, it would still show portions of the previous location.

    To remedy this, I did the following:

    Near the bottom of the MoveControl function, I added a line of code...

    m_pParent->ScreenToClient(rcBounds);
    pControl->MoveWindow(rcBounds);

    pControl->Invalidate (); // new line of code
    }

    UpdateParentSize();
    }
    }

    Hope this helps...
    Al

    Reply
  • GREAT

    Posted by Marcel.Kummer on 05/23/2005 05:26am

    Hi Paul! What an honest piece of code! It took me 4 minutes to add your class to my project, to append my controls using m_cControlPos.AddControl and to enable negative moves - and it worked well immediately! Thanks for this contribution! Marcel

    Reply
  • i want to create a main window that do not move

    Posted by Legacy on 01/27/2004 12:00am

    Originally posted by: hector

    help

    Reply
  • help!!!

    Posted by Legacy on 05/14/2003 12:00am

    Originally posted by: garfield

    Dear Sir:
    
    Thank u for you CControlPos first. I have some question about it.
    I want to set a button resize not only horizontal but also vertical, but it
    can’t realize. How should I do ?
    The code in my project:
    m_cControlPos.AddControl(IDC_FilePrint_BUTTON, CP_MOVE_HORIZONTAL);
    m_cControlPos.AddControl(IDCANCEL, CP_MOVE_HORIZONTAL);
    m_cControlPos.AddControl(IDC_FilePrint_BUTTON, CP_MOVE_VERTICAL);
    m_cControlPos.AddControl(IDCANCEL, CP_MOVE_VERTICAL);

    How can I realize that result such as the picture in attach.

    Reply
  • Little problem in ControlPos constructor

    Posted by Legacy on 05/23/2002 12:00am

    Originally posted by: GeSta

    This example is really great. THANX

    Just while implementing i find out
    that constructor ControlPos(*Parent) doesn't
    do everything correctly m_nOldParentHeight isn't set
    or smth...

    using standart
    CP = new ControlPos();
    CP.SetParent(myParent);

    everything goes OK.

    That's it... thax again

    GeSta


    Reply
  • weak code

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

    Originally posted by: sherl

    if set the one CListCtrl under another CListCtrl is that when
    you resize dialog, one CListCtrl overlapped with other CListCtrl

    Reply
  • Fix for resizing group boxes

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

    Originally posted by: Andy Saul

    When a group box control is used with the CControlPos class, the radio buttons or other grouped controls are redrawn incorrectly.
    
    

    This is because they are redrawn by both CControlPos::MoveControls function and are also repainted by the group box control.

    The easiest way to get around this is by not repainting them in CControlPos. Change the code at the bottom of MoveControls from

    m_pParent->ScreenToClient(rcBounds);
    pControl->MoveWindow(rcBounds);
    }
    UpdateParentSize();
    }

    to this:

    m_pParent->ScreenToClient(rcBounds);
    pControl->MoveWindow(rcBounds, FALSE); // Don't redraw yet!
    }
    // Let parent to redraw controls
    m_pParent->RedrawWindow(NULL, NULL,
    RDW_ALLCHILDREN |RDW_INVALIDATE |
    RDW_UPDATENOW | RDW_ERASE);
    UpdateParentSize();
    }

    It is important to set the RDW_ALLCHILDREN flag so that every control is updated.

    It is also important that the parent dialog does NOT have the WS_CLIPCHILDREN style, as this will prevent the group box background from being redrawn.

    In order to move the group boxes in the first place, they must be given a unique ID instead of the IDC_STATIC that is provided by default (even though a group box is a CButton, not a CStatic object...).

    Cheers!
    Andy Saul

    Reply
  • Much appreciation

    Posted by Legacy on 11/22/2001 12:00am

    Originally posted by: Ian

    I just had to bash in this kind of thing into an urgent prototype and this code was a godsend.

    Very fast to hook up, which was especially useful because I had to cascade resizing through three or four generations of child windows.

    And it works. Praise the lord, it works.

    A butt saved, and it was mine. Thanks, mate.

    Kia ora
    Ian

    Reply
  • Nice and Easy

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

    Originally posted by: Dave

    30 minutes I had my CDialogBar controls auto-sizing and moving. Class is easy to use - worked first time and integrates easily into your app.

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

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds