HOWTO: Center a Dialog Using the Microsoft Foundation Classes

ID: Q102328


The information in this article applies to:


SUMMARY

In an application developed with the Microsoft Foundation Classes library, you may want to automatically center a dialog box with respect to its parent window when it is created.

To do this, it is necessary to ensure that the dialog box can be centered without regard to its parent window (to support the case in which the caller specified NULL as the parent window handle). You must add additional code to center the dialog box with respect to the desktop window when no parent is specified and with respect to the parent window otherwise.


MORE INFORMATION

The sample code below can be used as an example to override the OnInitDialog() member function of the CDialog class. The CenterDialog() function, defined below, is compatible with version 1.0 of the Microsoft Foundation Classes library.

If you develop your application with version 2.0 or later of the Microsoft Foundation Classes library, use the CenterWindow() function instead of the CenterDialog() function. CenterWindow() provides identical functionality, but it is not available in version 1.0 of the MFC library.

The MFC version 2.0 implementation of the CenterWindow() function can be found in the DLGCORE.CPP file (installed by default in the C:\MFC\SRC directory). In MFC version 3.0 and above, CenterWindow() is a member function of CWnd which is the base class of CDialog. In these MFC versions, the implementation of CWnd::CenterWindow() can be found in WINCORE.CPP (installed by default in the MFC\SRC subdirectory of the corresponding Visual C++ product directory).

Sample Code

   /*
    * Compiler options needed: (Standard Windows Requirements)
    */ 

   void CMyDialog::CenterDialog(CWnd *MyDialogPtr)
   {
      CPoint   Point;
      CRect    DialogRect;
      CRect    ParentRect;
      int      nWidth;
      int      nHeight;
      CWnd     *DesktopWindow = NULL;
      CWnd     *MainWindow;

      // Get the size of the dialog box.
      MyDialogPtr->GetWindowRect(DialogRect);

      // Get the main window.
      MainWindow = AfxGetApp()->m_pMainWnd;

      // Determine if the main window exists. This can be useful when
      // the application creates the dialog box before it creates the
      // main window. If it does exist, retrieve its size to center
      // the dialog box with respect to the main window.
      if (MainWindow != NULL)
         MainWindow->GetClientRect(ParentRect);
      // If the main window does not exist, center with respect to
      // the desktop window.
      else
      {
         DesktopWindow = MyDialogPtr->GetDesktopWindow();
         DesktopWindow->GetWindowRect(ParentRect);
      }

      // Calculate the height and width for MoveWindow().
      nWidth = DialogRect.Width();
      nHeight = DialogRect.Height();

      // Find the center point and convert to screen coordinates.
      Point.x = ParentRect.Width() / 2;
      Point.y = ParentRect.Height() / 2;
      if (MainWindow)
         MainWindow->ClientToScreen(&Point);
      else
         DesktopWindow->ClientToScreen(&Point);

      // Calculate the new X, Y starting point.
      Point.x -= nWidth / 2;
      Point.y -= nHeight / 2;

      // Move the window.
      MyDialogPtr->MoveWindow(Point.x, Point.y, nWidth, nHeight, FALSE);
   }

   BOOL CMyDialog::OnInitDialog()
   {
      CDialog::OnInitDialog();

      // Remove the comment from 1 or 2 below to correspond with
      // the version of the Microsoft Foundation Classes library
      // you are using.

      // 1. Call CenterDialog() for version 1.0
      // CenterDialog(this);

      // 2. Call CenterWindow() for versions 2.0 and above
      // CenterWindow();

      return TRUE;
   } 

Additional query words:


Keywords          : kbMFC KbUIDesign kbVC 
Version           : 1.0 1.5 1.51 1.52 2.0 2.1 4.0 7.
Platform          : MS-DOS NT WINDOWS 
Issue type        : 

Last Reviewed: July 30, 1999