Microsoft KB Archive/154006

= How To Drag a Form by a Child Control =

Article ID: 154006

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Enterprise Edition

-



This article was previously published under Q154006



SUMMARY
A window can be moved by clicking the Title bar and then dragging the window to the required position. This technique will not work if the window doesn't have a Title bar. Using the SendMessage API, you can achieve this effect by dragging a control on the form and the form will move with the control. Below is a code sample showing how to accomplish this.



MORE INFORMATION
 Start a new project in Visual Basic. Form1 is created by default. Place a Command button on the form.  Add the following code to the Form1 code window: Option Explicit Private Declare Sub ReleaseCapture Lib "User" Private Declare Function SendMessage Lib "User" (ByVal hWnd As _     Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _      lParam As Any) As Long

Private Const WM_SYSCOMMAND = &H112 Private Const MOUSE_MOVE = &HF012

' Enter the line below as one line of code Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, _   X As Single, Y As Single) Dim lReturn As Long Call ReleaseCapture lReturn = SendMessage(Form1.hWnd, WM_SYSCOMMAND, MOUSE_MOVE, 0) End Sub  Press the F5 key to run the project. Note that if you drag the Command button on the screen, the whole form will move with it.

You could remove the Title bar by adjusting the Control box, Caption, and Borderstyle properties of the form and the form can still be dragged by the control.

