WIN32项目中MFC程序窗口居中

//class CMainWindow : public CFrameWnd

void CMainWindow::OnSize(UINT nType, int cx, int cy)
{
    CFrameWnd::OnSize(nType, cx, cy);

    // TODO: 在此处添加消息处理程序代码
    // SIZE_RESTORED   Window has been resized, but neither SIZE_MINIMIZED nor SIZE_MAXIMIZED applies.
    if( nType == SIZE_RESTORED )
    {
        /* 获取屏幕大小 */
        int iScreenW = GetSystemMetrics( SM_CXSCREEN );
        int iScreenH = GetSystemMetrics( SM_CYSCREEN );

        /* 获取窗口大小 */
        RECT rcWindowRect;
        GetWindowRect( &rcWindowRect );

        int iWindowW = rcWindowRect.right - rcWindowRect.left;
        int iWindowH = rcWindowRect.bottom - rcWindowRect.top;

        /* 获取任务栏高度 */
        RECT rect;
        HWND hwndTaskbar;
        int iTaskbarH;
        hwndTaskbar = ::FindWindow(L"Shell_TrayWnd", 0);
        ::GetWindowRect(hwndTaskbar, &rect);
        iTaskbarH = rect.bottom - rect.top;

        int iLeft = (iScreenW - iWindowW ) / 2;
        int iTop = (iScreenH - iWindowH - iTaskbarH ) / 2;

        MoveWindow( iLeft, iTop, iWindowW, iWindowH, FALSE ); 
    }
}

posted @ 2013-11-06 11:41  xingrun  阅读(999)  评论(0编辑  收藏  举报