2016-06-19 46 views
-1

我在它的父面板內有一個面板,允許我移動。我希望它停止移動,然後它會脫離父面板。什麼是完成這個最好的方法。我也動態地添加面板。面板邊緣檢測,在出界之前停止

更新: 以下是進入「MyPanel」面板的代碼。 「MyPanel」和「Panel」之間的區別僅在於我添加了一個邊框並且可以移動它。 「CoolMove」來自他人在網上找到的答案。我添加一個「MyPanel1」來形成,然後添加另一個「MyPanel2」,並允許它只在「MyPanel1」上移動。因此,我希望「MyPanel2」完全保留在「MyPanel1」的範圍內。我正在努力獲得正確的代碼來實現這一點。

Private allowCoolMove As Boolean = False 
Private myCoolPoint As New Point 
Public Overridable Sub MyPanel_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown 
    'If panel is ontop of Stock panel, then allow manual moving 
    If Me.Parent.Name.StartsWith("S") Then 
     allowCoolMove = True 
     myCoolPoint = New Point(e.X, e.Y) 
     Me.Cursor = Cursors.SizeAll 
     Me.BringToFront() 
    ElseIf Not Me.Parent.Name.Contains("keyR") Then 
     DoDragDrop(Me, DragDropEffects.Move) 
    End If 
End Sub 

Private Sub MyPanel_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove 
    If allowCoolMove = True Then 
     Me.Location = New Point(Me.Location.X + e.X - myCoolPoint.X, Me.Location.Y + e.Y - myCoolPoint.Y) 
    End If 
End Sub 

Private Sub MyPanel_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp 
    allowCoolMove = False 
    Me.Cursor = Cursors.Default 
End Sub 

回答

0

每個控制具有ClientRectangle property返回它的客戶區(對於一個面板,是內部部分)的尺寸。還有一個DisplayRectangle property,它告訴你整個整個區域的控件。

而且Rectangle結構具有Contains method重載,另需Rectangle結構,並告訴你是否一個矩形另一個矩形的界限內完全包含。

現在,您應該能夠將這兩個事實放在一起來提出解決問題的代碼。例如:

Dim rcParentPanelInterior As Rectangle = parentPanel.ClientRectangle 
Dim rcChildPanel   As Rectangle = childPanel.DisplayRectangle 

If rcParentPanelInterior.Contains(rcChildPanel) 
    ' continue to allow moving 
Else 
    ' forbid moving 
End If 
+0

我無法正常工作。我把它放在MouseMove事件中(這是我的移動代碼的地方),它只是阻止它移動? – goomba454

+0

@ goomba454:請用您的新代碼更新您的問題。 –

+0

好吧,我更新了我正在嘗試使用的代碼。 – goomba454