I know you can make a form moveable by setting moveable to true. However if you make the control box property false and delete the caption then at runtime you cant make the form moveable is there a way I can make a form moveable through code?
Printable View
I know you can make a form moveable by setting moveable to true. However if you make the control box property false and delete the caption then at runtime you cant make the form moveable is there a way I can make a form moveable through code?
Here's what's involved:
VB Code:
Option Explicit Public Const WM_NCLBUTTONDOWN = &HA1 Private Const HTCAPTION = 2 Public Declare Function ReleaseCapture Lib "user32" () As Long Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Public Sub DragForm(frm As Form) '================================= On Local Error Resume Next Call ReleaseCapture Call SendMessage(frm.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0) End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) '================================================ If Button = vbLeftButton Then Call DragForm(Me) End If End Sub
I'm not sure, but I think you can simply use the
With (Form)
.Top
.Left
End With
To modify the position...
Quote:
Originally posted by Walter100
I know you can make a form moveable by setting moveable to true. However if you make the control box property false and delete the caption then at runtime you cant make the form moveable is there a way I can make a form moveable through code?
A simple way is:
public mX, mY
in the mousemove event:
if button = 1 then move left+x-mx, top+y-my
in the mousedown event
mx = x
my = y
I inserted the code into a new project and received errors :eek:
Try my code.
Change all Public declarations to Private and run it.
never mind I have gotten it to work thanks everyone :)
Yours works digital error thanks :)
You're welcome.
Try
Code:Dim OldX As Single
Dim OldY As Single
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
OldX = X
OldY = Y
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then Move Left + (X - OldX), Top + (Y - OldY)
End Sub