

Public Class Form1
'Declare the variables
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
drag = True 'Sets the variable drag to true.
mousex = Windows.Forms.Cursor.Position.X - Me.Left 'Sets variable mousex
mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
'If drag is set to true then move the form accordingly.
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
End Sub
End Class
Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
Capture = False
WndProc(Message.Create(Handle, &HA1, New IntPtr(2), IntPtr.Zero))
End Sub
Protected Overrides Sub WndProc(ByRef Msg As Message)
If (Msg.Msg = &H84) Then
Dim _Point As Point = PointToClient(New Point(CInt(Msg.LParam.ToInt64 + &HFFFF), CInt((Msg.LParam.ToInt64 + &HFFFF0000) >> &H10)))
Dim _Size As Size = ClientSize
If (_Point.X >= _Size.Width - &H10 AndAlso _Point.Y >= _Size.Height - &H10 AndAlso _Size.Height >= &H10) Then
Msg.Result = If(IsMirrored, New IntPtr(&H10), New IntPtr(&H11))
Return
End If
End If
MyBase.WndProc(Msg)
End Sub
#Region "Move Form"
Const WM_NCHITTEST As Integer = &H84
Const HTCLIENT As Integer = &H1
Const HTCAPTION As Integer = &H2
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_NCHITTEST
MyBase.WndProc(m)
If m.Result = HTCLIENT Then m.Result = HTCAPTION
Case Else
MyBase.WndProc(m)
End Select
End Sub
#End Region
Public Class Form1
Private MausLocation As Point
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
MausLocation = e.Location
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.Location = e.Location - MausLocation + Me.Location
End If
End Sub
End Class
