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:
Private Sub pnlCaption_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlCaption.MouseDown If e.Button = Windows.Forms.MouseButtons.Left And Me.WindowState <> FormWindowState.Maximized Then MoveForm() End If End Sub Private Sub MoveForm() ReleaseCapture() SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0) 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:
Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown If e.Button = Windows.Forms.MouseButtons.Left And Me.WindowState <> FormWindowState.Maximized Then ResizeForm(resizeDir) End If End Sub Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove 'Calculate which direction to resize based on mouse position If e.Location.X < BorderWidth And e.Location.Y < BorderWidth Then resizeDir = ResizeDirection.TopLeft ElseIf e.Location.X < BorderWidth And e.Location.Y > Me.Height - BorderWidth Then resizeDir = ResizeDirection.BottomLeft ElseIf e.Location.X > Me.Width - BorderWidth And e.Location.Y > Me.Height - BorderWidth Then resizeDir = ResizeDirection.BottomRight ElseIf e.Location.X > Me.Width - BorderWidth And e.Location.Y < BorderWidth Then resizeDir = ResizeDirection.TopRight ElseIf e.Location.X < BorderWidth Then resizeDir = ResizeDirection.Left ElseIf e.Location.X > Me.Width - BorderWidth Then resizeDir = ResizeDirection.Right ElseIf e.Location.Y < BorderWidth Then resizeDir = ResizeDirection.Top ElseIf e.Location.Y > Me.Height - BorderWidth Then resizeDir = ResizeDirection.Bottom Else resizeDir = ResizeDirection.None End If End Sub Private Sub ResizeForm(ByVal direction As ResizeDirection) Dim dir As Integer = -1 Select Case direction Case ResizeDirection.Left dir = HTLEFT Case ResizeDirection.TopLeft dir = HTTOPLEFT Case ResizeDirection.Top dir = HTTOP Case ResizeDirection.TopRight dir = HTTOPRIGHT Case ResizeDirection.Right dir = HTRIGHT Case ResizeDirection.BottomRight dir = HTBOTTOMRIGHT Case ResizeDirection.Bottom dir = HTBOTTOM Case ResizeDirection.BottomLeft dir = HTBOTTOMLEFT End Select If dir <> -1 Then ReleaseCapture() SendMessage(Me.Handle, WM_NCLBUTTONDOWN, dir, 0) End If 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!





Reply With Quote