Automatically Center the Controls in CFormView | CodeGuru

Automatically Center the Controls in CFormView

Click here for larger image Environment: VC6 For a CFormView application, when the form size is changed, the margins around the controls in the form may appear to be unbalanced. When the form application is used on very different monitor resolutions, the same problem also appear. To make the controls automatically centralize while the form […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 25, 2001
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





Click here for larger image

Environment: VC6

For a CFormView application, when the form size is changed, the margins around the controls in the form may appear to be unbalanced. When the form application is used on very different monitor resolutions, the same problem also appear. To make the controls automatically centralize while the form is resizing, the following procedure can be used:

  1. Put an invisible group box around all the controls in the form (make sure the tab order of the group box is the last).
  2. Add the handler for the message WM_SIZE (using the class wizard) OnSize(UINT nType, int cx, int cy) to the form view.
  3. Use the following code to the OnSize function:
void myFormView::OnSize(UINT nType, int cx, int cy)
{
  CFormView::OnSize(nType, cx, cy);
  // Don’t adjust position when scrollbars appear.
  if ((GetScrollPos(SB_HORZ) != 0) || (GetScrollPos(SB_VERT) != 0))
  {
    return;
  }
  int   topMargin = 20, leftMargin = 20;
  CRect rectView, rectTotal;
  this  GetWindowRect(&rectView);
  for (CWnd *wnd = GetWindow(GW_CHILD); wnd != NULL;
    wnd = wnd->GetWindow(GW_HWNDNEXT))
  {
      CWnd *pWnd = GetDlgItem(IDC_TOTAL_RECT);
      pWnd->GetWindowRect(&rectTotal);
      CRect rect;
      int xPos, yPos;
      wnd->GetWindowRect(&rect);
      if(rectView.Width()>(rectTotal.Width() + 2*leftMargin))
      {
        xPos = (rectView.Width() – rectTotal.Width())/2;
      }
      else
      {
        xPos = leftMargin;
      }
      if(rectView.Height()>(rectTotal.Height() + 2*topMargin))
      {
        yPos = (rectView.Height() – rectTotal.Height())/2;
      }
      else
      {
        yPos = topMargin;
      }
      wnd->MoveWindow(xPos + rect.left – rectTotal.left,
        yPos + rect.top – rectTotal.top,
        rect.Width(), rect.Height(), TRUE);
    }
}

Downloads

Download source & demo project – 50 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.