Class to Auto-Position Controls on Window Resize | CodeGuru

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, […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 3, 2000
1 minute read
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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.