作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 CDialog 的 OnInitDialog 期间获取 cx 和 cy。
我可以用下面的代码做到这一点:
myDialog::OnInitDialog()
{
CRect rcWindow2;
this->GetWindowRect(rcWindow2);
CSize m_szMinimum = rcWindow2.Size();
int width = m_szMinimum.cx;
int height = m_szMinimum.cy;
}
但是,OnInitDialog中的cx和cy与进入OnSize的cx和cy并不相同:
void myDialog::OnSize(UINT nType, int cx, int cy)
来自 OnInitDialog:cx=417,cy=348
来自 OnSize:cx=401,cy=310
看起来可能是边界,但我无法弄清楚。
关于如何在 OnInitDialog 中获得与输入 OnSize 相同的 xy 数据的建议将不胜感激。
继承:
myDialog -> CDialog -> CWnd
最佳答案
GetWindowRect
返回窗口在屏幕坐标中的左上角位置。窗口的宽度和高度包括边框粗细和标题的高度。
GetClientRect
始终为左上角返回零。宽度和高度与 OnSize
的值相同。
虽然我们在讨论这个主题,但在移动子窗口时,这也会让人感到困惑。因为 SetWindowPos
需要客户端坐标,而 GetWindowRect
只返回屏幕坐标。需要像这样进行屏幕/客户端转换:
void GetWndRect(CRect &rc, HWND child, HWND parent)
{
GetWindowRect(child, &rc);
CPoint offset(0, 0);
ClientToScreen(parent, &offset);
rc.OffsetRect(-offset);
}
现在我们可以在对话框中移动一个按钮:
CWnd *child = GetDlgItem(IDOK);
CRect rc;
GetWndRect(rc, child->m_hWnd, m_hWnd);
rc.OffsetRect(-5, -5);
child->SetWindowPos(0, rc.left, rc.top, 0, 0, SWP_NOSIZE);
关于c++ - 为什么来自 GetWindowRect(rcWindow2) 的 cx/cy 与馈入 OnSize 的 cx/cy 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29616771/
我是一名优秀的程序员,十分优秀!