How to move windows when the mouse is down

we can move windows when we click on the title bar. but how can I move windows when the mouse is in shape?

+3
source share
3 answers

You will need to record when the mouse is up and down using the MouseDown and MouseUp :

 private bool mouseIsDown = false; private Point firstPoint; private void Form1_MouseDown(object sender, MouseEventArgs e) { firstPoint = e.Location; mouseIsDown = true; } private void Form1_MouseUp(object sender, MouseEventArgs e) { mouseIsDown = false; } 

As you can see, the first point is recorded, so you can use the MouseMove as follows:

 private void Form1_MouseMove(object sender, MouseEventArgs e) { if (mouseIsDown) { // Get the difference between the two points int xDiff = firstPoint.X - e.Location.X; int yDiff = firstPoint.Y - e.Location.Y; // Set the new point int x = this.Location.X - xDiff; int y = this.Location.Y - yDiff; this.Location = new Point(x, y); } } 
+10
source

You can do this manually by handling the MouseDown event, as described in other answers. Another option is to use this small utility class that I wrote some time ago. This allows you to make the window "movable" automatically, without a line of code.

+4
source

Listen to the event when the mouse button is lowered in the form and then listens for the mouse movement until it returns up.

Here is a code article that shows how to do this: Move window / form without title in C #

+3
source

All Articles