子窗口居中显示不正常

用SOUI做界面库,子窗口在弹出的时候,只是根据屏幕居中,而不是根据父窗口居中,查了下源码,发现用错了

   BOOL CenterWindow(HWND hWndCenter = NULL)
    {
        SASSERT(::IsWindow(m_hWnd));

        // determine owner window to center against
        DWORD dwStyle = GetStyle();
        if(hWndCenter == NULL)
        {
            if(dwStyle & WS_CHILD)
                hWndCenter = ::GetParent(m_hWnd);
            else
                hWndCenter = ::GetWindow(m_hWnd, GSW_OWNER);
                
            if(hWndCenter == NULL)
                hWndCenter = ::GetActiveWindow();
        }

        // get coordinates of the window relative to its parent
        RECT rcDlg;
        ::GetWindowRect(m_hWnd, &rcDlg);
        RECT rcArea;
        RECT rcCenter;
        HWND hWndParent;
        if(!(dwStyle & WS_CHILD))
        {
            // don't center against invisible or minimized windows
            if(hWndCenter != NULL)
            {
                DWORD dwStyleCenter = ::GetWindowLong(hWndCenter, GWL_STYLE);
                if(!(dwStyleCenter & WS_VISIBLE) || (dwStyleCenter & WS_MINIMIZE))
                    hWndCenter = NULL;
            }

            // center within screen coordinates
#if WINVER < 0x0500
            ::SystemParametersInfo(SPI_GETWORKAREA, NULL, &rcArea, NULL);
#else
            HMONITOR hMonitor = NULL;
            if(hWndCenter != NULL)
            {
                hMonitor = ::MonitorFromWindow(hWndCenter, MONITOR_DEFAULTTONEAREST);
            }
            else
            {
                hMonitor = ::MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST);
            }

            MONITORINFO minfo;
            minfo.cbSize = sizeof(MONITORINFO);
            ::GetMonitorInfo(hMonitor, &minfo);

            rcArea = minfo.rcWork;
#endif
            if(hWndCenter == NULL)
                rcCenter = rcArea;
            else
                ::GetWindowRect(hWndCenter, &rcCenter);
        }
        else
        {
            // center within parent client coordinates
            hWndParent = ::GetParent(m_hWnd);
            SASSERT(::IsWindow(hWndParent));

            ::GetClientRect(hWndParent, &rcArea);
            SASSERT(::IsWindow(hWndCenter));
            ::GetClientRect(hWndCenter, &rcCenter);
            ::MapWindowPoints(hWndCenter, hWndParent, (POINT*)&rcCenter, 2);
        }

        int DlgWidth = rcDlg.right - rcDlg.left;
        int DlgHeight = rcDlg.bottom - rcDlg.top;

        // find dialog's upper left based on rcCenter
        int xLeft = (rcCenter.left + rcCenter.right) / 2 - DlgWidth / 2;
        int yTop = (rcCenter.top + rcCenter.bottom) / 2 - DlgHeight / 2;

        // if the dialog is outside the screen, move it inside
        if(xLeft + DlgWidth > rcArea.right)
            xLeft = rcArea.right - DlgWidth;
        if(xLeft < rcArea.left)
            xLeft = rcArea.left;

        if(yTop + DlgHeight > rcArea.bottom)
            yTop = rcArea.bottom - DlgHeight;
        if(yTop < rcArea.top)
            yTop = rcArea.top;

        // map screen coordinates to child coordinates
        return ::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1,
                              SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
    }


原先是 CenterWindow( m_hChild ).

应该改为CenterWindow( NULL ).


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值