我有一个窗口,我知道内容大小已经确定,我希望它居中,并且在选择菜单选项时大小也会改变。尽管我阅读了文档,但我对如何将GetWindowRect()
、AdjustWindowRect()
和MoveWindow()
函数一起使用感到困惑,如果更有经验的成员能够提供一个示例进行解释,我将感谢
感谢所有花时间帮助的人
@IInspectable的注释之后。我创建了以下方法,它可以为各种客户端大小的菜单选项正确地居中和调整大小
BOOL MainWindow::AdjustWindow()
{
//use required fields to set rectangle of client area
RECT clientToWindowRect;
clientToWindowRect.left = GetScreenCenter().x - GetClientSize().cx / 2;
clientToWindowRect.top = GetScreenCenter().y - GetClientSize().cy / 2;
clientToWindowRect.right = GetScreenCenter().x + GetClientSize().cx / 2;
clientToWindowRect.bottom = GetScreenCenter().y + GetClientSize().cy / 2;
//send it to AdjustWindowRect()
if (!AdjustWindowRect(&clientToWindowRect,
WS_OVERLAPPEDWINDOW | WS_MINIMIZEBOX |
WS_SYSMENU | WS_CLIPCHILDREN | WS_VISIBLE,
TRUE))
{
GetLastError();
return FALSE;
}
//use rectangle in MoveWindow()
if (!MoveWindow(this->Window(),
CalculateCenter(this->Window(),
clientToWindowRect).x,
CalculateCenter(this->Window(),
clientToWindowRect).y,
MeasureRect(clientToWindowRect).cx,
MeasureRect(clientToWindowRect).cy,
TRUE))
{
GetLastError();
return FALSE;
}
return TRUE;
}