Move forms without using the title bar
By: Unknown Date: 24/09/1998

This source code example allows you the ability to move a form around on the screen (using the mouse) without dragging the form's title bar.

In order to test this example, you will need only a blank form. Start Visual Basic and select New Project. On the newly created form simply choose View Code and insert the following constants and declarations in the form level:

Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1
Declare Function ReleaseCapture Lib "user32" Alias "ReleaseCapture" () As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

The next order of business it to actually trap the clicking of the left mouse button over the form's client area. To do this, we will use the MouseDown event. When the event is triggered, we will verify that it was the left mouse button that triggered the event, then call the Windows API ReleaseCapture Sub followed by sending the WM_NCLBUTTONDOWN constant message to the window. Enter the code into the Form_MouseDown event as follows:

Sub Form_MouseDown (Button As Integer, Shift As Integer, x As Single, Y As Single)
    If Button = 1 Then  'Checking for Left Button only
       Dim ReturnVal%
       ReleaseCapture
       ReturnVal% = SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    End If
End Sub

Run the project, and click the button over the form and drag it around!

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
Webmaster's e-mail: webmaster@vbprogzone.cjb.net
Hosted by InWind: www.inwind.it