Results 1 to 7 of 7

Thread: Drag & Drop form

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    8

    Question

    Does anyone know how to make form so you can drag and drop them accross the desktop without clicking on the blue title bar.

    Cheers

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Option Explicit
    Code:
    Option Explicit
    
    'Make A Titleless Form Moveable With Mouse Drag
    'Add the following code to the declarations section of a form
    
    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_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
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Guest
    To do it without API:

    Code:
    Dim oldX As Long, oldY As Long, isMoving As Boolean
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    oldX = X
    oldY = Y
    isMoving = True
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If isMoving Then
        Me.Top = Me.Top - (oldY - Y)
        Me.Left = Me.Left - (oldX - X)
    End If
    End Sub
    
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    isMoving = False
    End Sub

  4. #4
    Guest
    Code:
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    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 Const WM_SYSCOMMAND = &H112
    Private Const SC_MOVE = &HF012
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ReleaseCapture
        SendMessage hwnd, WM_SYSCOMMAND, SC_MOVE, 0
    End Sub

  5. #5
    Guest
    Matthew:

    you can shorten your code by disregarding the boolean flag and by using the Move method.
    Code:
    Dim prevX As Single, prevY As Single
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        prevX = X
        prevY = Y
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = 1 Then Move Left - (prevX - X), Top - (prevY - Y)
    End Sub

  6. #6
    Guest

    Thumbs up

    Thanks Megatron...nice, short, and it works just the same .

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    8

    Talking

    Thanks everyone, the code worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width