开发者

How to drag and move winform using mouse

I know how to 'drag and move' a winform by adding following code

Protected Overrides Sub WndProc(ByRef m As Message)
    If (((m.Msg = 163) And ClientRectangle.Contains(PointToClient(New Point(m.LParam.ToInt32)))) And (m.WParam.ToInt32 = 2)) Then
        m.WParam = CType(1, IntPtr)
    End If
    MyBase.WndProc(m)
    If ((m.Msg = 132) And (m.Result.ToInt32 = 1)) Then
        m.Result = CType(2, IntPtr)
    End If
End Sub

But after a panel being added to winform, I can not 'drag and move' the winform within that panel area. Any idea of how to 'drag and move' within a panel? I mean the mouse point, click, hold and move within that panel and the winform will follow the mouse movement until I release the mouse button.

Update: The solution to my problem.

'Add these to your form class
Private MouseIsDown As Boolean = False
Private MouseIsDownLoc As Point = Nothing

'This is the MouseMove event of your panel
Private Sub panel_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Panel3.MouseMove
    If开发者_运维百科 e.Button = MouseButtons.Left Then
        If MouseIsDown = False Then
            MouseIsDown = True
            MouseIsDownLoc = New Point(e.X, e.Y)
        End If

        Me.Location = New Point(Me.Location.X + e.X - MouseIsDownLoc.X, Me.Location.Y + e.Y - MouseIsDownLoc.Y)
    End If
End Sub

'And the MouseUp event of your panel
Private Sub panel_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Panel3.MouseUp
    MouseIsDown = False
End Sub


EDIT: Changed to VB.NET - I really need to start reading the tags...

'Add these to your form class
Private MouseIsDown As Boolean = False
Private MouseIsDownLoc As Point = Nothing

'This is the MouseMove event of your panel
Private Sub panel_MouseMove(sender As Object, e As MouseEventArgs)
    If e.Button = MouseButtons.Left Then
        If MouseIsDown = False Then
            MouseIsDown = True
            MouseIsDownLoc = New Point(e.X, e.Y)
        End If

        Me.Location = New Point(Me.Location.X + e.X - MouseIsDownLoc.X, Me.Location.Y + e.Y - MouseIsDownLoc.Y)
    End If
End Sub

'And the MouseUp event of your panel
Private Sub panel_MouseUp(sender As Object, e As MouseEventArgs)
    MouseIsDown = False
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜