Centring a Dialog on the Screen

Date: 10 June 1997
Author: Jeff Lawson

Try this:

void TMyDialog::CenterWindow(void)
{
  // center the dialog on the screen
  TRect dRect = GetWindowRect();
  int width = ::GetSystemMetrics(SM_CXSCREEN);
  int height = ::GetSystemMetrics(SM_CYSCREEN);
  TPoint topLeft((width - dRect.Width()) / 2,(height - dRect.Height()) / 2);
  TRect newPosition(topLeft,dRect.Size());
  MoveWindow(newPosition, true);
}

or, if your target platform is 32-bit only, you can just add the DS_CENTER style to your dialog in the resource editor.

See Also: Getting the Size of the Screen