Results 1 to 40 of 40

Thread: Move and Resize a Control or a Borderless Form - using window messages (smooth!)

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Move and Resize a Control or a Borderless Form - using window messages (smooth!)

    Hi,

    Here is a small example of how to move and resize a form without a border (FormBorderStyle = None).

    Instead of manually setting the Location and Size of the Form, I have decided to use the SendMessage function to handle it. This results in a much smoother experience, (especially with lots of controls) and will also make sure that the form always follows the mouse, regardless of how fast you move it. In other examples you will often notice that if you move your mouse too fast, the form can no longer keep up and will stop moving.


    The example uses a Panel as the form's caption. Dragging the panel will move the form, just like the real caption of a form with a border would behave.
    You can of course decide to use a different control. You could even use the form itself. I just thought a Panel was the easiest example.

    For resizing, I did not use any special controls; I merely use a BorderWith constant (default: 6) that defines the width of the area you can use to resize the form.

    Screenshot:

    (You can of course use nice icons for the buttons instead, but that was not the purpose of this codebank submission)



    The form moving works pretty simple: when the MouseDown event of the Panel is fired, a simple check is done to make sure that the user clicked the Left button instead of the Right button, and that the form is not maximized (you cannot move a maximized form).
    Then, a MoveForm() method is called, in which the SendMessage function is called with the correct parameters.
    vb.net Code:
    1. Private Sub pnlCaption_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlCaption.MouseDown
    2.         If e.Button = Windows.Forms.MouseButtons.Left And Me.WindowState <> FormWindowState.Maximized Then
    3.             MoveForm()
    4.         End If
    5.     End Sub
    6.  
    7.     Private Sub MoveForm()
    8.         ReleaseCapture()
    9.         SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    10.     End Sub


    The resizing is a little more involved, as it requires a check for the position of the mouse cursor, and changes the cursor to a resizing cursor.
    For this, I made a Property resizeDir which changes the cursor depending on which value it gets automatically.
    The value of this property is used in a ResizeForm method, which calls the SendMessage function again with the correct parameters (depending on the position of the mouse, or the resizeDir value).
    vb.net Code:
    1. Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    2.         If e.Button = Windows.Forms.MouseButtons.Left And Me.WindowState <> FormWindowState.Maximized Then
    3.             ResizeForm(resizeDir)
    4.         End If
    5.     End Sub
    6.  
    7.     Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    8.         'Calculate which direction to resize based on mouse position
    9.  
    10.         If e.Location.X < BorderWidth And e.Location.Y < BorderWidth Then
    11.             resizeDir = ResizeDirection.TopLeft
    12.  
    13.         ElseIf e.Location.X < BorderWidth And e.Location.Y > Me.Height - BorderWidth Then
    14.             resizeDir = ResizeDirection.BottomLeft
    15.  
    16.         ElseIf e.Location.X > Me.Width - BorderWidth And e.Location.Y > Me.Height - BorderWidth Then
    17.             resizeDir = ResizeDirection.BottomRight
    18.  
    19.         ElseIf e.Location.X > Me.Width - BorderWidth And e.Location.Y < BorderWidth Then
    20.             resizeDir = ResizeDirection.TopRight
    21.  
    22.         ElseIf e.Location.X < BorderWidth Then
    23.             resizeDir = ResizeDirection.Left
    24.  
    25.         ElseIf e.Location.X > Me.Width - BorderWidth Then
    26.             resizeDir = ResizeDirection.Right
    27.  
    28.         ElseIf e.Location.Y < BorderWidth Then
    29.             resizeDir = ResizeDirection.Top
    30.  
    31.         ElseIf e.Location.Y > Me.Height - BorderWidth Then
    32.             resizeDir = ResizeDirection.Bottom
    33.  
    34.         Else
    35.             resizeDir = ResizeDirection.None
    36.         End If
    37.     End Sub
    38.  
    39. Private Sub ResizeForm(ByVal direction As ResizeDirection)
    40.         Dim dir As Integer = -1
    41.         Select Case direction
    42.             Case ResizeDirection.Left
    43.                 dir = HTLEFT
    44.             Case ResizeDirection.TopLeft
    45.                 dir = HTTOPLEFT
    46.             Case ResizeDirection.Top
    47.                 dir = HTTOP
    48.             Case ResizeDirection.TopRight
    49.                 dir = HTTOPRIGHT
    50.             Case ResizeDirection.Right
    51.                 dir = HTRIGHT
    52.             Case ResizeDirection.BottomRight
    53.                 dir = HTBOTTOMRIGHT
    54.             Case ResizeDirection.Bottom
    55.                 dir = HTBOTTOM
    56.             Case ResizeDirection.BottomLeft
    57.                 dir = HTBOTTOMLEFT
    58.         End Select
    59.  
    60.         If dir <> -1 Then
    61.             ReleaseCapture()
    62.             SendMessage(Me.Handle, WM_NCLBUTTONDOWN, dir, 0)
    63.         End If
    64.     End Sub


    I attached the full example (without any compiled files, so you will probably need to Build it first) so you can play around with it.

    Enjoy!
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width