tags:

views:

73

answers:

3

we able to move windows forms when we mouse down on title bar . but how can I move windows when mouse down in form ?

+2  A: 

Listen for the event when the mouse button goes down in the form and then listen for mouse moves until it goes up again.

Here's a codeproject article that shows how to do this: Move window/form without Titlebar in C#

ho1
+1  A: 

You'll need to record when the mouse is down and up using the MouseDown and MouseUp events:

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 being recorded, so you can then use the MouseMove event 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);
    }
}
GenericTypeTea
+3  A: 

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

Thomas Levesque