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


Comments

  • Very nice! But the group box is not needed if you add this...

    Posted by Legacy on 12/16/2003 12:00am

    Originally posted by: Cristina Ca�ero

    I found your article very, very useful! Thank you so much!
    
    

    However, I had a lot of forms in my application, and I
    didn't wanted to add a group box to each of them, and fix
    the tab order (I am a very lazy person)

    Instead, I added this to your code:


    // Auto-detects the limits
    bool first = true;
    for (CWnd *pWnd = GetWindow(GW_CHILD); pWnd != NULL; pWnd = pWnd->GetWindow(GW_HWNDNEXT)) {
    CRect rect;

    pWnd->GetWindowRect(&rect);

    if (first) {
    rectTotal.bottom = rect.bottom;
    rectTotal.top = rect.top;
    rectTotal.right = rect.right;
    rectTotal.left = rect.left;
    first = false;
    } else {
    rectTotal.bottom = max(rectTotal.bottom,rect.bottom);
    rectTotal.top = min(rectTotal.top,rect.top);
    rectTotal.right = max(rectTotal.right,rect.right);
    rectTotal.left = min(rectTotal.left,rect.left);
    }
    }


    And hence, I commented this part of your code...


    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;

    Reply
  • Great idea! - Slight optimization

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

    Originally posted by: Mike Petry

    The code would run faster (and be easier to understand)if the rectangle of IDC_TOTAL_RECT is obtained once, before entering the for loop.

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds