PRODUCT : Borland C++ NUMBER : 869 VERSION : All OS : WIN DATE : October 19, 1993 PAGE : 1/3 TITLE : How To Center A Dialog Box On Its Parent Window It is often necessary to center a dialog box on top of its parent window for aesthetic reasons. Who would one do this? Simple, use the function provided below when the dialog box that needs centering receives a WM_INITDIALOG message. Below is a sample dialog box procedure that shows the use of the function UICenterDialog as well as the source for UICenterDialog itself. Note: If the dialog box would not be 100% visible by centering it on top of its parent window, this code then centers it on the Windows desktop. If this behavior is not necessary simply remove the last if statement. // CNTRDLG.C // Copyright (C) 1992 Borland Internationa, Inc. // All Rights Reserved // Function to show the use of UICenterDialog //............................................................... BOOL FAR PASCAL MyDialogProc(HWND hDlg, WORD msg, WORD wParam, LONG lParam) { switch(msg) { case WM_INITDIALOG: UICenterDialog(hDlg); break; default: return TRUE; } return FALSE; } // Source for the UICenterDialog function //............................................................... void UICenterDialog(HWND hDlg) { int cxWin, // Delta X for parent window cyWin, // Delta Y for parent window PRODUCT : Borland C++ NUMBER : 869 VERSION : All OS : WIN DATE : October 19, 1993 PAGE : 2/3 TITLE : How To Center A Dialog Box On Its Parent Window xDlg, // New X coordinate for dialog box yDlg, // New Y coordinate for dialog box cxDlg, // Delta X for dialog box cyDlg, // Delta Y for dialog box cxScreen, // Delta X for screen cyScreen; // Delta Y for screen RECT rcDlg, // Rectangle for dialog box rcPrnt; // Rectangle for parent window HWND hWndParent; // Handle the parent window // Determine the width and height of the screen. cxScreen = GetSystemMetrics(SM_CXSCREEN); cyScreen = GetSystemMetrics(SM_CYSCREEN); // Determine rectangle for parent window of dialog box. hWndParent = GetWindowWord(hDlg, GWW_HWNDPARENT); if(IsWindow(hWndParent)) { // Use parent coordinates (rect) to determine where the // dialog needs to be on the Windows desktop GetWindowRect(hWndParent, &rcPrnt); cxWin = rcPrnt.right - rcPrnt.left; cyWin = rcPrnt.bottom - rcPrnt.top; } else { // We were unable to determine who the parent is, // therefore centering the dialog is pointless! Place // it in the upper left corner of the Windows desktop. cxWin = 0; // Force the centering calculation to cyWin = 0; // think that the dialog is centered on // the Windows desktop } // Determine rectangle for dialog box itself. GetWindowRect(hDlg, &rcDlg); cxDlg = rcDlg.right - rcDlg.left; cyDlg = rcDlg.bottom - rcDlg.top; // Determine new dialog X and Y coordinate xDlg = (cxWin - cxDlg) / 2; xDlg += rcPrnt.left; yDlg = (cyWin - cyDlg) / 2; PRODUCT : Borland C++ NUMBER : 869 VERSION : All OS : WIN DATE : October 19, 1993 PAGE : 3/3 TITLE : How To Center A Dialog Box On Its Parent Window yDlg += rcPrnt.top; // Center the dialog using new coordinates if ((xDlg < 0) || (yDlg < 0) || (xDlg+cxDlg > cxScreen) || (yDlg+cyDlg > cyScreen)) { // If by centering the dialog on top of its parent it // would not be 100% visible, then center it on the // Windows desktop instead. xDlg = (cxScreen - cxDlg) / 2; yDlg = (cyScreen - cyDlg) / 2; } // Move the dialog box to its new home on the desktop MoveWindow(hDlg, xDlg, yDlg, cxDlg, cyDlg, TRUE); } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.