This article was previously published under Q173773
This article demonstrates how to move a form with the mouse when the formhas no title bar by using the 32-bit Windows API SendMessage function withthe WM_NCLBUTTONDOWN message.
The WM_NCLBUTTON message is generated by Windows when a user right-clickson a window's title bar. Windows uses this message to determine whether thewindow should be moved by further user actions. If the title bar does notexist, this message is not generated. However, it is often useful to move awindow when it doesn't have a title bar. A window or form in 32-bit VisualBasic will not have a title bar under the following circumstances:
The sample code below illustrates a way of letting users move a formwithout a title bar by simply clicking and dragging it from anywhere on theform. The code placed in the MouseMove event procedure of Form1 may beplaced in other event procedures, if so desired. For example, the codecould be placed in the MouseDown event procedure of a Label controlallowing movement of the form by selecting and dragging from the Labelcontrol.
NOTE: In Visual Basic 4.0, a form with a menu will have the title barsupplied by default so the method demonstrated below is not necessary. InVisual Basic 5.0 and Visual Basic 6.0, a form with a menu will not have atitle bar supplied by default.
Start a new Standard EXE project. Form1 is added by default.
Set Form1's ControlBox, MinButton, and MaxButton properties to False, and then clear its Caption.
Place a CommandButton control (Command1) on Form1.
Place the following code in the General Declarations section of Form1:
Private Declare Function SendMessage Lib "User32" _ Alias "SendMessageA" (ByVal hWnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Private Declare Sub ReleaseCapture Lib "User32" () Const WM_NCLBUTTONDOWN = &HA1 Const HTCAPTION = 2 Private Sub Form_Load() Command1.Caption = "Exit" End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _ X As Single, Y As Single) Dim lngReturnValue As Long If Button = 1 Then Call ReleaseCapture lngReturnValue = SendMessage(Form1.hWnd, WM_NCLBUTTONDOWN, _ HTCAPTION, 0&) End If End Sub Private Sub Command1_Click() End End Sub
Press the F5 key to run the program. Clicking and holding over Form1 will allow you to move the form. Pressing the CommandButton control will exit the program.
For information on how to move a form without a title bar in 16-bitversions of Visual Basic, please see the following article in the MicrosoftKnowledge Base:
114593 : How To Move a Form that Has No Titlebar or Caption