Class to Auto-Position Controls on Window Resize


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


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.


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()
  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);


Download source - 4 Kb
Download demo project - 17 Kb


  • Sr. Project Manager

    Posted by dvfds on 06/10/2014 04:31pm

    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.

  • One small modification...

    Posted by adinelt on 03/27/2006 10: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...


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


    Hope this helps...


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

    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

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

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

    Originally posted by: hector


  • help!!!

    Posted by Legacy on 05/14/2003 07: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.

  • Little problem in ControlPos constructor

    Posted by Legacy on 05/23/2002 07: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();

    everything goes OK.

    That's it... thax again


  • weak code

    Posted by Legacy on 01/04/2002 08: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

  • Fix for resizing group boxes

    Posted by Legacy on 12/12/2001 08: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


    to this:

    pControl->MoveWindow(rcBounds, FALSE); // Don't redraw yet!
    // Let parent to redraw controls
    m_pParent->RedrawWindow(NULL, NULL,

    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...).

    Andy Saul

  • Much appreciation

    Posted by Legacy on 11/22/2001 08: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

  • Nice and Easy

    Posted by Legacy on 08/08/2001 07: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.

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date