我在其中允许移动的父面板中有一个面板。我希望它在它脱离父面板之前停止移动。实现这一目标的最佳方法是什么?我还动态添加了面板。
更新: 这是进入“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 :(得分: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