【问题标题】:How to drag and move winform using mouse如何使用鼠标拖动和移动winform
【发布时间】:2011-05-28 15:06:14
【问题描述】:

我知道如何通过添加以下代码来“拖动和移动”winform

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

但是在将面板添加到 winform 后,我无法在该面板区域内“拖动和移动”winform。关于如何在面板中“拖动和移动”的任何想法?我的意思是鼠标指向、单击、按住并在该面板内移动,winform 将跟随鼠标移动,直到我释放鼠标按钮。

更新:我的问题的解决方案。

'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

【问题讨论】:

    标签: .net vb.net winforms visual-studio-2010 mouse


    【解决方案1】:

    编辑:更改为 VB.NET - 我真的需要开始阅读标签...

    '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
    

    【讨论】:

    • 我的错。您的代码确实有效。我只需要添加与我的面板名称匹配的 Handles 子句。非常感谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    相关资源
    最近更新 更多