2011-05-28 95 views
3

我知道如何拖曳和移动“一个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。有关如何在面板中“拖动”的想法?我的意思是鼠标指向,单击,按住并在该面板内移动,并且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 

回答

4

编辑:改到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 
+0

对不起,你的代码无法正常工作。 – user774411 2011-05-28 16:16:13

+0

我的不好。你的代码实际上工作。我只需要添加与我的面板名称匹配的句柄子句。非常感谢 !!! – user774411 2011-05-28 16:29:08

+0

没问题 - 很高兴你得到它的工作! – 2011-05-28 16:38:15