2011-05-28 93 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