Microsoft KB Archive/74798

{|
 * width="100%"|

HOWTO: Center a Dialog Box on the Screen

 * }

Q74798

-

The information in this article applies to:


 * Microsoft Windows Software Development Kit (SDK)
 * Microsoft Win32 Software Development Kit (SDK), used with:
 * the operating system: Microsoft Windows NT, versions 3.5, 3.51

-

SUMMARY
When an application developed for the Microsoft Windows graphical environment displays a dialog box, centering the dialog box on the screen is sometimes desirable. However, on systems with high- resolution displays, the application displaying the dialog box may be nowhere near the center of the screen. In these cases, it is preferable to place the dialog near the application requesting input.

To center a dialog box on the screen before it is visible, add the following lines to the processing of the WM_INITDIALOG message:

   { RECT rc;

GetWindowRect(hDlg, &rc);

SetWindowPos(hDlg, NULL,      ((GetSystemMetrics(SM_CXSCREEN) - (rc.right - rc.left)) / 2),       ((GetSystemMetrics(SM_CYSCREEN) - (rc.bottom - rc.top)) / 2),       0, 0, SWP_NOSIZE | SWP_NOACTIVATE); } This code centers the dialog horizontally and vertically.

Under Windows 95 or later and NT 4.0 or later, use the style DS_CENTER to get the same effect.

Additional query words:

Keywords : kbDlg kbOSWinNT350 kbOSWinNT351 kbSDKWin32 kbGrpDSUser

Issue type : kbhowto

Technology : kbWin32SDKSearch kbAudDeveloper kbSDKSearch kbWin32sSearch kbWinSDKSearch