Article ID: 140722
Article Last Modified on 7/11/2005
BOOL NEAR PASCAL TestNotify(HWND hDlg, LPOFNOTIFY pofn)
{
switch (pofn->hdr.code)
{
case CDN_INITDONE:
{
CenterWindow (GetParent (hDlg), NULL);
break;
}
:
:
}
// Center with respect to another window.
// Specifying NULL for hwndParent centers hwndChild relative to the
// screen.
BOOL CenterWindow(HWND hwndChild, HWND hwndParent)
{
RECT rcChild, rcParent;
int cxChild, cyChild, cxParent, cyParent;
int cxScreen, cyScreen, xNew, yNew;
HDC hdc;
// Get the Height and Width of the child window.
GetWindowRect(hwndChild, &rcChild);
cxChild = rcChild.right - rcChild.left;
cyChild = rcChild.bottom - rcChild.top;
if (hwndParent)
{
// Get the Height and Width of the parent window.
GetWindowRect(hwndParent, &rcParent);
cxParent = rcParent.right - rcParent.left;
cyParent = rcParent.bottom - rcParent.top;
}
else
{
cxParent = GetSystemMetrics (SM_CXSCREEN);
cyParent = GetSystemMetrics (SM_CYSCREEN);
rcParent.left = 0;
rcParent.top = 0;
rcParent.right = cxParent;
rcParent.bottom= cyParent;
}
// Get the display limits.
hdc = GetDC(hwndChild);
cxScreen = GetDeviceCaps(hdc, HORZRES);
cyScreen = GetDeviceCaps(hdc, VERTRES);
ReleaseDC(hwndChild, hdc);
// Calculate new X position, then adjust for screen.
xNew = rcParent.left + ((cxParent - cxChild) / 2);
if (xNew < 0)
{
xNew = 0;
}
else if ((xNew + cxChild) > cxScreen)
{
xNew = cxScreen - cxChild;
}
// Calculate new Y position, then adjust for screen.
yNew = rcParent.top + ((cyParent - cyChild) / 2);
if (yNew < 0)
{
yNew = 0;
}
else if ((yNew + cyChild) > cyScreen)
{
yNew = cyScreen - cyChild;
}
// Set it, and return.
return SetWindowPos(hwndChild,
NULL,
xNew, yNew,
0, 0,
SWP_NOSIZE | SWP_NOZORDER);
}
Keywords: kbcmndlgfileo kbcmndlg kbprb kbcmndlgsave KB140722