moveable form

ÜnLoCo

Member
Joined
Jul 18, 2009
Messages
8
Programming Experience
Beginner
hello
here's how to make a moveable form in case you let off the title bar
(formborderstyle = none )

2103i.jpg


VB.NET:
Dim down, init, curpt As Point

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        init = Location
        'record the form (initial) position 
        down = System.Windows.Forms.Control.MousePosition()
        'record the cursor (initial) position 
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            curpt = System.Windows.Forms.Control.MousePosition()
            'get the current pos of the mouse cursor (step = every pixel)
            Location = init + (curpt - down)
            'simply add the difference(+/-) to the initial pos of the form
        End If
    End Sub

if you have a better idea plz post it
 
Code below is a common method, you can use it in combination with checking which button is clicked and where, or perhaps for MouseDown of a custom title panel.
VB.NET:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    Me.Capture = False
    Const WM_NCLBUTTONDOWN As Integer = &HA1
    Const HTCAPTION As Integer = 2
    Me.DefWndProc(Message.Create(Me.Handle, WM_NCLBUTTONDOWN, New IntPtr(HTCAPTION), IntPtr.Zero))
End Sub
 
Back
Top