您的位置:首页 > 其它

MFC按客户区大小获取合适的主窗口大小

2017-05-01 14:36 197 查看

使用GetClientRect与GetWindowRect

计算水平、垂直非客户区大小

使用GetWindowRect获取主窗口的大小, 再通过GetClientRect获取客户端大小,然后通过这两个值计算出水平、垂直非客户区占的大小

计算主窗口大小

由上一步计算得到的非客户区大小加大期望的客户区大小,即为主窗口的大小

以下是设置非客户区大小为500*300的例子:

CRect clientRect;
CRect windowRect;
GetClientRect( clientRect );
GetWindowRect( windowRect );
int cxNoClient = windowRect.Width() - clientRect.Width();
int cyNoClient = windowRect.Height() - clientRect.Height();
int cxWindow2 = 500 + cxNoClient;
int cyWindow2 = 300 + cyNoClient;
SetWindowPos( NULL, 0,0, cxWindow2, cyWindow2, SWP_NOMOVE );


使用API函数AdjustWindowRect/AdjustWindowRectEx

此API函数根据指定的非客户区大小、是否有菜单、窗口样式去自动计算出窗口所占大小,挺方便的。具体的还是移步到MSDN上去查看相关说明,这里仅举个例子(例子是在主窗口的OnCreate函数中编写,直接引用lpCreateStruct中的变量值):

CRect tmpRect( 0, 0, 500, 300 );
AdjustWindowRectEx( tmpRect, lpCreateStruct->style,
TRUE, lpCreateStruct->dwExStyle );
int cxWindow1 = tmpRect.Width();
int cyWindow1 = tmpRect.Height();
SetWindowPos( NULL, 0,0, cxWindow1, cyWindow1, SWP_NOMOVE );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: