简介
CWnd::CalcWindowRect (函数原型)
virtual void CalcWindowRect( LPRECT lpClientRect, UINT nAdjustType = ustBorder );
Parameters(参数)
lpClientRect
Points to a RECT structure or CRect object that contains the resultant value of the window rectangle.(指向RECT结构或CRect对象(包含window矩形的值)的
指针)
nAdjustType
An enumerated type used for in-place editing. It can have the following values:
一种枚举类型,可以有如下的值:
CWnd::adjustBorder = 0, which means that scroll-bar sizes are ignored in calculation;
当adjustBorder=0时,表示在计算时忽略滚动条大小
and CWnd::adjustOutside = 1, which means that they are added into the final measurements of the rectangle.
当adjustOutside=1时,表示把滚动条大小考虑进去
Remarks(备注)
Call this member function to compute the required size of the window rectangle based on the desired client-rectangle size. The resulting window rectangle (contained in lpClientRect) can then be passed to the Create member function to create a window whose client area is the desired size.
调用这个成员函数以根据所需的客户矩形大小计算窗口矩形的大小。随后算出的窗口矩形(保存在lpClientRect)中可以被传递到Create成员函数以创建一个窗口,其客户区大小就是要求的大小
Called by the framework to size windows prior to creation.
它被框架调用以确定产生窗口的大小
A client rectangle is the smallest rectangle that completely encloses a client area. A window rectangle is the smallest rectangle that completely encloses the window.、
客户矩形是紧绕客户区的最小矩形,窗口矩形是紧绕窗口的最小矩形
Example(举例)
// Uses CalcWindowRect to determine size for new CFrameWnd
// based on the size of the current view. The end result is a
// top level frame window of the same size as CMyView's frame.
用该函数计算基于当前视图尺寸的新框架窗口的大小,最后得到一个和CMyView框架尺寸相等的最上层框架窗口
void CMyView::OnMyCreateframe()
{
CFrameWnd* pFrameWnd = new CFrameWnd;
CRect myRect;
GetClientRect(myRect);
发展
pFrameWnd->CalcWindowRect(&myRect, CWnd::adjustBorder);
pFrameWnd->MoveWindow(0, 0, myRect.Width(), myRect.Height());
pFrameWnd->ShowWindow(SW_SHOW);
}
总的来说,该函数的功能就是使你能够精确地限定客户区矩形的大小。
比如你的客户区是200*200的,因为调用Create函数(或者CreateEx)的时候指定的窗口大小是窗口矩形的大小(窗口矩形包含客户区矩形和
标题栏,
菜单栏等),并且
标题栏菜单栏这些元素的尺寸随着
视频驱动程序和显示器图形分辨率的不同而不同,所以创建窗口时,无法准确限定客户区矩形的大小。
使用过程:
CreateEx(0,
strClassName,
strTitleName,
WS_CAPTION | WS_MINIMIZEBOX | WS_OVERLAPPED | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT, // 指定要创建的窗口矩形的Width
CW_USEDEFAULT, // 指定要创建的窗口矩形的Height
NULL,
NULL)
但是我们用到的客户区矩形可能只有200*200,(假设200<CW_USEDEFAULT- x , x 为非客户区所占的宽或高),为了低碳节约,我们再对窗口进行调整.假设调整后的窗口矩形为210*210,(其中包含200*200的客户区矩形,剩下的是
菜单,标题栏等等)
CRect rect (0, 0, 200, 200);
// 上面语句执行后 rect.width = 200, rect.height = 200.
CalcWindowRect (&rect);
// 上面语句执行后 rect.width = 210, rect.height = 210.
SetWindowPos(0, 0, 0,
rect.Width(),
rect.Heigth(),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW )
CalcWindowRect 需要在窗口创建之后进行调用,以能够使窗口的非客户区被考虑和生效。