duilib+mfc 移动父窗口

3 篇文章 0 订阅
3 篇文章 0 订阅

制作一个客户端,选择duilib+cef+mfc为客户端界面。父窗口为一个对话框,子窗口为duilib的窗口(Mainframe)。当主界面把mfc的对话框盖住时,发现窗口移动不了(无标题窗口)。为了移动主窗口,记录的解决方法:

  • 第一个解决方法(失败)
...
    if (uMsg == WM_LBUTTONUP)  
    {  
        POINT point = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };  
        ::ReleaseCapture();  
        ::SendMessage(::GetParent(this->GetHWND()),WM_SYSCOMMAND,SC_DRAGMOVE,0); 
        return 0;
    }
...

这段代码实现了点击窗口任意位置,窗口移动的目的,但是窗口的所有消息都被丢弃了(return 0)。不能采用这个方法。需要监控鼠标移动+鼠标点击的事件。
* 第二个方法(ok)

if (uMsg == WM_MOUSEMOVE)
    {  
        if (::GetAsyncKeyState(VK_LBUTTON) != 0 && m_bStartMove)  
        {   
            POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };  
            int dx = pt.x - m_StartPt.x;  
            int dy = pt.y - m_StartPt.y;  
            RECT curRect;  
            ::GetWindowRect(m_hWnd, &curRect);  

            int cx = curRect.left + dx;  
            int cy = curRect.top + dy;  

            int  w = curRect.right - curRect.left;  
            int  h = curRect.bottom - curRect.top;  

            SetWindowPos(::GetParent(this->GetHWND()), NULL, cx, cy, w, h, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);  
        }  

    }
    else if (uMsg == WM_LBUTTONDOWN)
    {  
        POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };  
        m_bStartMove = TRUE;  
        m_StartPt = pt;  
    }
    else if (uMsg == WM_LBUTTONUP)  
    {  
        m_bStartMove = FALSE;  
    }  

不需要任何返回。完整代码如下:

LRESULT MainFrame::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    LRESULT lRes = 0; 
    if (uMsg == WM_MOUSEMOVE)
    {  
        if (::GetAsyncKeyState(VK_LBUTTON) != 0 && m_bStartMove)  
        {   
            POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };  
            int dx = pt.x - m_StartPt.x;  
            int dy = pt.y - m_StartPt.y;  
            RECT curRect;  
            ::GetWindowRect(m_hWnd, &curRect);  

            int cx = curRect.left + dx;  
            int cy = curRect.top + dy;  

            int  w = curRect.right - curRect.left;  
            int  h = curRect.bottom - curRect.top;  

            SetWindowPos(::GetParent(this->GetHWND()), NULL, cx, cy, w, h, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);  
        }  

    }
    else if (uMsg == WM_LBUTTONDOWN)
    {  
        POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };  
        m_bStartMove = TRUE;  
        m_StartPt = pt;  
    }
    else if (uMsg == WM_LBUTTONUP)  
    {  
        m_bStartMove = FALSE;  
    }  
    if( uMsg == WM_CREATE ) 
    {  
        CPaintManagerUI::SetInstance(AfxGetInstanceHandle());//加载XML的时候,需要使用该句柄去定位EXE的路径,才能加载XML的路径
        CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T("skin"));//定位图片等资源的位置

        m_pm.Init(m_hWnd);  
        CDialogBuilder builder;  
        CControlUI *pRoot = builder.Create(_T("ui.xml"), (UINT)0, NULL, &m_pm); //加载的XML文件的名称
        ASSERT(pRoot && "Failed to parse XML");  
        m_pm.AttachDialog(pRoot);  
        m_pm.AddNotifier(this);  

        return 0;  
    }  
    else if( uMsg == WM_DESTROY ) 
    {  
        ::PostQuitMessage(0);  
    }  

    if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;  
    return CWindowWnd::HandleMessage(uMsg, wParam, lParam);

}

大功告成!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值