HOWTO: Drag a Form by a Child Control

Last reviewed: October 13, 1997
Article ID: Q154006
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit only, for Windows, version 4.0

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

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Place a Command button on the form.

  3. 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
    
    

  4. 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.

REFERENCES

For more information, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q103062
   TITLE     : How to Move Controls at Run Time By Using Drag and Drop

   ARTICLE-ID: Q113904
   TITLE     : How to Move a Control Across a Form at Run Time

   ARTICLE-ID: Q79884
   TITLE     : How to Move Controls Between Forms in VB for Windows
Keywords          : PrgOther vb416 VB4WIN
Version           : WINDOWS:4.0
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 13, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.