none
moving window with right mouse button

    Question

  • I'm using window without titlebar and I want to move it by dragging it with mouse.

    I tried:

             case WM_LBUTTONDOWN:
                
                 SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);

                 break;
                 
            case WM_RBUTTONDOWN:
                
                 SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);

                 break;

    but window moves fine with left mouse button, but it dosn't move with right one.

    Can someone please tell me what I'm doing wrong?

     

    Monday, January 09, 2006 12:47 PM

Answers

  • This trick does not work with the right mouse button, altough I've seen articles that says it does. I think Windows checks if the left mouse button was pressed in the mousemove, or something similar. The following piece of code however works fine, no matter if left or right is pressed:



    public partial class Form1 : Form
    {
        private bool m_Moving = false;
        private Point m_MousePosAtClick;
        public Form1()
        {
            InitializeComponent();
        }
        protected override void OnMouseDown( MouseEventArgs e )
        {
            m_Moving = true;
            m_MousePosAtClick = e.Location;
            base.OnMouseDown( e );
        }
        protected override void OnMouseUp( MouseEventArgs e )
        {
            m_Moving = false;
            base.OnMouseUp( e );
        }
        protected override void OnMouseMove( MouseEventArgs e )
        {
            if( m_Moving )
            {
                Point tmp = new Point();
                tmp.X = this.Location.X + ( e.Location.X - m_MousePosAtClick.X );
                tmp.Y = this.Location.Y + ( e.Location.Y - m_MousePosAtClick.Y );
                this.Location = tmp;
            }
            base.OnMouseMove( e );
        }
    }

     

    Tuesday, January 10, 2006 2:14 PM

All replies

  • I think you have to do everything. Handle right click down, when the mouse move, move the window and stop when the right click up if activated.
    Monday, January 09, 2006 7:18 PM
  • Why don't I have to do it with left mouse button?
    Tuesday, January 10, 2006 8:56 AM
  • You ask for a solution to do it with the right mouse button ?

    "but window moves fine with left mouse button, but it dosn't move with right one"

    Tuesday, January 10, 2006 1:17 PM
  • This trick does not work with the right mouse button, altough I've seen articles that says it does. I think Windows checks if the left mouse button was pressed in the mousemove, or something similar. The following piece of code however works fine, no matter if left or right is pressed:



    public partial class Form1 : Form
    {
        private bool m_Moving = false;
        private Point m_MousePosAtClick;
        public Form1()
        {
            InitializeComponent();
        }
        protected override void OnMouseDown( MouseEventArgs e )
        {
            m_Moving = true;
            m_MousePosAtClick = e.Location;
            base.OnMouseDown( e );
        }
        protected override void OnMouseUp( MouseEventArgs e )
        {
            m_Moving = false;
            base.OnMouseUp( e );
        }
        protected override void OnMouseMove( MouseEventArgs e )
        {
            if( m_Moving )
            {
                Point tmp = new Point();
                tmp.X = this.Location.X + ( e.Location.X - m_MousePosAtClick.X );
                tmp.Y = this.Location.Y + ( e.Location.Y - m_MousePosAtClick.Y );
                this.Location = tmp;
            }
            base.OnMouseMove( e );
        }
    }

     

    Tuesday, January 10, 2006 2:14 PM
  •  ThE_lOtUs wrote:

    You ask for a solution to do it with the right mouse button ?

    "but window moves fine with left mouse button, but it dosn't move with right one"

    I wanted to know why does this code:

    SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);

    work with left mouse button and doesn't work with right one.

    Tuesday, January 10, 2006 4:21 PM