虚函数 CalcWindowRect
每当主框架窗口的客户区尺寸发生变化或控制条的位置发生变化,需要重新排列客户区时,调用该函数,根据视图客户区尺寸计算视图窗口的尺寸。
我们知道,排列主窗口客户区是由CFrameWnd::RecalcLayout()完成的。显然,视图的CalcWindowRect()函数也是由它触发调用的。主窗口的客户区尺寸减掉所有控制占用的部分,剩下的区域分给视图,这部分区域作为实参传入CalcWindowRect()。在CalcWindowRect()函数内,需要计算视图窗口的尺寸。代码如下:
void CView::CalcWindowRect(LPRECT lpClientRect, UNIT nAdjustType)
{
// lpClientRect此时是整个视图客户区的尺寸
// 需要为滚动条增加尺寸吗
if (nAdjustType != 0)
{
// 调用API,根据窗口风格计算窗口尺寸
::AdjustWindowRectEx(lpClientRect, 0, FALSE, GetExStyle());
DWORD dwStyle = GetStyle();
if (dwStyle & WS_VSCROLL)
{
// 为垂直滚动条增加尺寸
int nAdjust = afxData.csVScroll;
if (dwStyle & WS_BORDER)
nAdjust -= CX_BORDER;
lpClientRect->right += nAdjust;
}
if (dwStyle & WS_HSCROLL)
{
// 为水平滚动条增加尺寸
int nAdjust = afxData.cyHScroll;
if (dwStyle & WS_BORDER)
nAdjust -= CY_BORDER;
lpClientRect->bottom += nAdjust;
}
return;
}
// 无需为滚动条增加尺寸,调用基类成员完成计算
CWnd::CalcWindowRect(lpClientRect, nAdjustType);
}