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
Printable View
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
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
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
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
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
Thanks Megatron...nice, short, and it works just the same :rolleyes:.
Thanks everyone, the code worked perfectly.