How do I move a window with the right mouse button using C ++?

I need to move the window with the right mouse button. The window has no title, no title. Left button works

 void CMyHud::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    SendMessage(WM_SYSCOMMAND, SC_MOVE|0x0002);
    CDialogEx::OnLButtonDown(nFlags, point);
}

      

But if I put this code on OnRButtonDown it doesn't work. What is the problem?

Well the solution is found thanks to Mark Ransom:

 CRect pos;

   void CMyHud::OnRButtonDown(UINT nFlags, CPoint point)
    {
        pos.left = point.x;
        pos.top = point.y;
        ::SetCapture(m_hWnd);

        CDialogEx::OnRButtonDown(nFlags, point);
    }


    void CMyHud::OnMouseMove(UINT nFlags, CPoint point)
    {
        CWnd* pWnd = CWnd::FromHandle(m_hWnd);
        CRect r;
        if(GetCapture() == pWnd)
        {
            POINT pt;
            GetCursorPos(&pt);
            GetWindowRect(r);
            pt.x -= pos.left;
            pt.y -= pos.top;
            MoveWindow(pt.x, pt.y, r.Width(), r.Height(),TRUE);
        }

        CDialogEx::OnMouseMove(nFlags, point);
    }


    void CMyHud::OnRButtonUp(UINT nFlags, CPoint point)
    {
        ReleaseCapture();

        CDialogEx::OnRButtonUp(nFlags, point);
    }

      

+3


source to share


3 answers


Your features OnRButtonDown

do SetCapture

that all mouse messages were forwarded to your window when the mouse button is pressed, also save the mouse position in a member variable. Now, in your function OnMouseMove

, check if the GetCapture

object returns with the same HWND as yours - if so, calculate the difference between the current mouse position and the saved one, then call MoveWindow

to move the window.



+3


source


As for the left click:

SC_MOVE|0x0002

is displayed as 0xF012

or SC_DRAGMOVE

. This appears to be an undocumented constant. There is probably a good reason Microsoft doesn't want anyone to use this, so they hid it.

Also WM_SYSCOMMAND

is a notification message. You should answer it, not send it. To drag a window with a left click:



message map ...
ON_WM_NCHITTEST()

LRESULT CMyDialog::OnNcHitTest(CPoint p)
{
    //responding to a notification message
    return HTCAPTION; 
}

      

To drag the window with the right mouse button, you must override OnRButtonDown, OnMouseMove, OnRButtonUp and create your own procedure. But the window behavior is very confusing. Is it really worth it?

+1


source


you can use the mouse to understand. WM_RBUTTONDOWN, WM_MOUSEMOVE

0


source







All Articles