Class to Auto-Position Controls on Window Resize

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.



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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read