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 ); 
    }
}

转载于:https://www.cnblogs.com/xingrun/p/3410187.html

你可能感兴趣的:(WIN32项目中MFC程序窗口居中)