2016-06-19 42 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