//获取屏幕尺寸
scrWidth = GetSystemMetrics(SM_CXSCREEN);
scrHeight = GetSystemMetrics(SM_CYSCREEN);
//获取窗体尺寸
CRect rect;
// ::GetWindowRect(m_hWnd, &rect);
rect.right = WindowWidth;// 设置窗口宽高
rect.bottom = WindowHeight;
rect.left = (scrWidth - rect.right) / 2;
rect.top = (scrHeight - rect.bottom) / 2;
// 设置窗体位置 (居中显示)
// SetWindowPos(m_hWnd, rect.left, rect.top, rect.right, rect.bottom, SWP_SHOWWINDOW);// HWND_TOP,`此句效果不对
MoveWindow(rect.left, rect.top, rect.right, rect.bottom, 1);// 居中
GetSystemMetrics 获取系统属性 SM_CXSCREEN / SM_CYSCREEN 代表屏幕的宽高
GetWindowRect 获取窗口位置
MoveWindow 移动窗口位置
ShowWindow(handler, SW_SHOW); 显示窗体
ShowWindow(handler, SW_HIDE); · 隐藏窗体
本文介绍了如何使用C++在Windows环境下实现窗口的居中显示及尺寸调整,包括获取屏幕尺寸、设置窗体尺寸和位置的方法。通过GetSystemMetrics、GetWindowRect和MoveWindow等API函数,可以轻松实现窗口的位置调整。
734

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



