duilib+mfc 移動父視窗
阿新 • • 發佈:2018-11-05
製作一個客戶端,選擇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);
}
大功告成!