I am making a project where the borderstyle of the form is 0. how do i make it move with a label and it it called label1
EDIT: Resolved!
Printable View
I am making a project where the borderstyle of the form is 0. how do i make it move with a label and it it called label1
EDIT: Resolved!
Heres 3 ways to do that,
' Using APIs
' Using VB code only #1Code:Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
ReleaseCapture
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
' note code continues here on mouse up.
End If
End Sub
' Using VB code only #2Code:Option Explicit
Private lngX As Long
Private lngY As Long
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
lngX = X
lngY = Y
End If
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then Me.Move Me.Left + X - lngX, Me.Top + Y - lngY
End Sub
Code:Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then Label1.Tag = X & "|" & Y
End Sub
Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1_MouseMove Button, Shift, X, Y
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lngLeft As Long, lngTop As Long, sngX As Single, sngY As Single
If Button = vbLeftButton And LenB(Label1.Tag) Then
sngX = Split(Label1.Tag, "|")(0)
sngY = Split(Label1.Tag, "|")(1)
lngLeft = Me.Left + X - sngX
lngTop = Me.Top + Y - sngY
Me.Move lngLeft, lngTop
End If
End Sub
Thank you!:)
Also set the Form property to this:
Code:Form1.Moveable = True
If this is solved to your satisfaction, could you please do us a little favour, and mark the thread as Resolved? (editing the first post to add that word is far from obvious)
(this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)
You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).