There is more than one way to move a form without a title bar. For instance using API’s and Subclassing. But you can also use regular code and simply track the position of the mouse at certain events and can thus move the form without Subclassing. The regular code way is what I actually prefer and will show in this article.
The basics of the source code is keeping track of the mouse location when the form’s _MouseDown event is fired and in the form’s _MouseMove event while the left Mouse-Button is still active. The first sets of codes will be for Visual Basic .NET including Visual Basic 2005/08 and Newer. Visual Basic 6.0 codes are further below.
Visual Basic.NET, VB 2008, VB.NET 2010, and Higher…
Put these in your programs Declaration area.
' 'These should go into your projects Declare area. ' Dim moveStartX As Integer Dim moveStartY As Integer Dim moveEndX As Integer Dim moveEndY As Integer
The variables above will hold the mouse starting position for when the left button is pressed and the ending position for when the mouse is moved.
Next, you want to go to your Forms_MouseDown() event…
Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles Me.MouseDown ' 'Keep track of the mouse starting position when the mouse button is pressed. ' moveStartX = e.X moveStartY = e.Y End Sub
The final set of codes needs to go into your Forms_MouseMove() event…
Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles Me.MouseMove ' 'Calculate the mouse positions in relation to its starting position. ' moveEndX = e.X - moveStartX moveEndY = e.Y - moveStartY ' 'Make sure the Left Mouse Button is pressed and then move the form to its new location. ' If e.Button = Windows.Forms.MouseButtons.Left Then Me.Location = New Point(moveEndX + Me.Location.X, moveEndY + Me.Location.Y) End If End Sub
So, all of the VB.NET codes above will move your Form pretty much the same way as if you had moved the form with the Titlebar..
Visual Basic 6.0
You can put these in the Declaration area.
' 'Declare the variables that will hold the mouse locations. This is almost exactly the same 'as .net but the variables types needs to be changed from Integer to Long since the 'Visual Basic 5.0/6.0 32Bit variable is Long and DotNets is Integer. ' Dim moveStartX As Long Dim moveStartY As Long Dim moveEndX As Long Dim moveEndY As Long
These go in the Forms_MouseDown event.
' 'You want to put these codes in your Forms_MouseDown event. This will keep track of 'where the mouse is positioned when the button is clicked. ' moveStartX = X moveStartY = Y
Then these go in the Forms_MouseMove Event.
' 'Put the codes below in your Forms_MouseMove event… ' 'Calculate the mouse position from its starting point to its current location. moveEndX = X - moveStartX moveEndY = Y - moveStartY If Button = 1 Then Me.Left = Me.Left + moveEndX Me.Top = Me.Top + moveEndY End If
That is all you have to do to get your form moveable if you have a non-title based form design. Have fun!
Minor Revision: 2015