Search This Blog

Thursday, March 6, 2014

How to move a windows form in .net

To move a windows form in .net 

  • Create a procedure to move the form when it is dragged. Enter code similar to the following to create a new Point object. This will act as a variable when you calculate how to move the form. The isMouseDown field is used to track whether the user is holding the mouse button down. The form should move only when the mouse is held down.
' Visual Basic
Private mouseOffset As Point
Private isMouseDown As Boolean = False
/ C#
/private Point mouseOffset;
private bool isMouseDown = false;
  • Create an event handler for the form's MouseDown event. In the handler, add code that allows a user to click anywhere on the form to drag it. 
Enter code similar to the following to assign coordinates to the mouseOffset variable based on the current position of the mouse pointer. In the code below, notice that the offset position is calculated using system information about the border size (FrameBorderSize.Width) and title bar height(CaptionHeight). These must be taken into account when testing the offset, because some measurements are made with the client area and some are made with screen coordinates. Thus, the offset is equal to the border width plus the caption height plus the offset into the client area of the form.
' Visual Basic
Private Sub Form1_MouseDown(ByVal sender As Object, _
    ByVal e As MouseEventArgs) Handles MyBase.MouseDown
    Dim xOffset As Integer
    Dim yOffset As Integer

    If e.Button = MouseButtons.Left Then
        xOffset = -e.X - SystemInformation.FrameBorderSize.Width
        yOffset = -e.Y - SystemInformation.CaptionHeight - _
                SystemInformation.FrameBorderSize.Height
        mouseOffset = New Point(xOffset, yOffset)
        isMouseDown = True
    End If
End Sub

// C#
private void Form1_MouseDown(object sender, 
    System.Windows.Forms.MouseEventArgs e)
{
    int xOffset;
    int yOffset;

    if (e.Button == MouseButtons.Left) 
    {
        xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
        yOffset = -e.Y - SystemInformation.CaptionHeight - 
            SystemInformation.FrameBorderSize.Height;
        mouseOffset = new Point(xOffset, yOffset);
        isMouseDown = true;
    }    
}

  • Create an event handler for the form's MouseMove event.
    Enter code similar to the following. When the left mouse button is clicked and the mouse is dragged, the form's Location property is set to the new position.
' Visual Basic
Private Sub Form1_MouseMove(ByVal sender As Object, _
    ByVal e As MouseEventArgs) Handles MyBase.MouseMove
    If isMouseDown Then
        Dim mousePos As Point = Control.MousePosition
        mousePos.Offset(mouseOffset.X, mouseOffset.Y)
        Location = mousePos
    End If
End Sub

// C#
private void Form1_MouseMove(object sender, 
    System.Windows.Forms.MouseEventArgs e)
{
    if (isMouseDown) 
    {
        Point mousePos = Control.MousePosition;
        mousePos.Offset(mouseOffset.X, mouseOffset.Y);
        Location = mousePos;
    }
}
  • Create an event handler for the form's MouseUp event. Enter code similar to the following.
' Visual Basic
Private Sub Form1_MouseUp(ByVal sender As Object, _
    ByVal e As MouseEventArgs) Handles MyBase.MouseUp
    ' Changes the isMouseDown field so that the form does
    ' not move unless the user is pressing the left mouse button.
    If e.Button = MouseButtons.Left Then
        isMouseDown = False
    End If
End Sub

// C#
private void Form1_MouseUp(object sender, 
    System.Windows.Forms.MouseEventArgs e)
{
    // Changes the isMouseDown field so that the form does
    // not move unless the user is pressing the left mouse button.
    if (e.Button == MouseButtons.Left) 
    {
        isMouseDown = false;
    }
}

Hope this post will help you.

No comments:

Post a Comment