|
void CenterWindow(HWND hWnd)
{
HWND hParentOrOwner;
RECT rc, rc2;
int x,y;
if((hParentOrOwner=GetParent(hWnd))==NULL)
{
SystemParametersInfo(SPI_GETWORKAREA,0,&rc,0);
}
else
{
GetClientRect(hParentOrOwner, &rc);
}
GetWindowRect(hWnd, &rc2);
x = ((rc.right-rc.left) - (rc2.right-rc2.left)) / 2 +rc.left;
y = ((rc.bottom-rc.top) - (rc2.bottom-rc2.top)) / 2 +rc.top;
SetWindowPos(hWnd,HWND_TOP,x, y,0, 0,SWP_NOSIZE);
}
|
MFC中CenterWindow()函数的模拟
本文介绍了一个用于将Windows窗口居中的实用函数。该函数首先检查窗口是否有父窗口,然后根据实际情况获取工作区或父窗口的尺寸,并据此计算目标窗口的位置坐标,最终通过调整窗口位置实现居中效果。

被折叠的 条评论
为什么被折叠?



