入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2017-05-01 投稿人: 浏览次数:1150
  • 计算水平、垂直非客户区大小
    使用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函数根据指定的非客户区大小、是否有菜单、窗口样式去自动计算出窗口所占大小,挺方便的。具体的还是移步到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 );
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。