3

I need to move window by right mouse button. The window has no caption, titlebar. By left button it 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 place this code on OnRButtonDown it dosen'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);
    }
Nika_Rika
  • 613
  • 2
  • 6
  • 29

3 Answers3

3

In your OnRButtonDown function, do a SetCapture to ensure all mouse messages are routed to your window while the mouse button is down. Also store the mouse position in a member variable. Now, in your OnMouseMove function, check to see if GetCapture returns an object with the same HWND as yours - if it does, calculate the difference between the current mouse position and the saved one, then call MoveWindow to move the window.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

In regards to left-mouse click:

SC_MOVE|0x0002 comes out as 0xF012 or SC_DRAGMOVE. This is apparently an undocumented constant. There is probably a good reason Microsoft doesn't want anybody to use this, that's why they have hidden it.

Also WM_SYSCOMMAND is a notification message. You should respond to it, not send it. To drag the window with left-mouse click:

message map ...
ON_WM_NCHITTEST()

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

To drag the window with right-mouse you have to override OnRButtonDown, OnMouseMove, OnRButtonUp and make your own routine. But Window's behaviour gets very confusing. Is that really worth it?

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • With left mouse button it works perfectly. I need right button. The solution is found by your and marks advice. Thanks! – Nika_Rika May 29 '15 at 05:39
0

you can use mouse message to realize. WM_RBUTTONDOWN, WM_MOUSEMOVE

allen
  • 21
  • 2