How to Move Form

Posts 1–4 of 4 · Page 1 of 1
How to Move Form
I need help with moving a form that has no titlebar.
I want to be able to grab a label and drag the form around with it.

Ive tried this but it doesnt work.

Code:
Dim DragOn As Boolean
Dim OldX As Single
Dim OldY As Single
Dim NewX As Single
Dim NewY As Single

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    DragOn = True
    OldX = X
    OldY = Y
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    DragOn = False
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If DragOn Then
        NewX = X
        NewY = Y
        Me.Top = Me.Top + (NewY - OldY)
        Me.Left = Me.Left + (NewX - OldX)
        OldX = NewX
        OldY = NewY
    End If
End Sub
and this..

Code:
Option Explicit

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 Function ReleaseCapture Lib "user32" () As Long

Private Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1

Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ReleaseCapture
    SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End Sub


Try that and tell me results.
Quote Originally Posted by Pixie View Post


Try that and tell me results.
Yeah I think it´s the same!
Quote Originally Posted by MinimalKills View Post
I need help with moving a form that has no titlebar.
I want to be able to grab a label and drag the form around with it.

Ive tried this but it doesnt work.

Code:
Dim DragOn As Boolean
Dim OldX As Single
Dim OldY As Single
Dim NewX As Single
Dim NewY As Single

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    DragOn = True
    OldX = X
    OldY = Y
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    DragOn = False
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If DragOn Then
        NewX = X
        NewY = Y
        Me.Top = Me.Top + (NewY - OldY)
        Me.Left = Me.Left + (NewX - OldX)
        OldX = NewX
        OldY = NewY
    End If
End Sub
and this..

Code:
Option Explicit

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 Function ReleaseCapture Lib "user32" () As Long

Private Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1

Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ReleaseCapture
    SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End Sub
Code:
Const WM_NCHITTEST As Integer = &H84
    Const HCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2



    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If m.Result = HCLIENT Then m.Result = HTCAPTION
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub
Thanks me if i helped
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us