拖放后将图片框捕捉到按钮



我正在制作一款战舰游戏,我使用按钮作为网格(游戏板)。我用一个图片框作为飞船,我试图让它与碰撞的按钮对齐。

我已经完成了拖放和碰撞的部分,但我正在努力捕捉按钮的部分。图片框有两个按钮那么大。我试着用picturebox.left=button.left将picturebox与按钮对齐,但它在两个按钮中选错了按钮。

Dim Off As Point
Private Sub picDestroyer_MouseDown(sender As Object, e As MouseEventArgs) Handles picDestroyer.MouseDown
Off.X = MousePosition.X - sender.Left 'Click and Drag ship
Off.Y = MousePosition.Y - sender.Top
End Sub
Private Sub picDestroyer_MouseMove(sender As Object, e As MouseEventArgs) Handles picDestroyer.MouseMove
If e.Button = MouseButtons.Left Then
sender.Left = MousePosition.X - Off.X 'Click and Drag ship
sender.Top = MousePosition.Y - Off.Y
End If
End Sub
Private Sub picDestroyer_DoubleClick(sender As Object, e As EventArgs) Handles picDestroyer.DoubleClick
If picDestroyer.Size = New Size(52, 21) Then 'Rotate Pic if double clicked
picDestroyer.Size = New Size(21, 52)
ElseIf picDestroyer.Size = New Size(21, 52) Then
picDestroyer.Size = New Size(52, 21)
End If
End Sub
Private Sub picDestroyer_MouseLeave(sender As Object, e As EventArgs) Handles picDestroyer.MouseLeave
For Each button In Me.Controls
If picDestroyer.Bounds.IntersectsWith(button.bounds) Then
button.backcolor = Color.Red
picDestroyer.BackColor = SystemColors.Control
End If
If picDestroyer.Bounds.IntersectsWith(button.bounds) And picDestroyer.Size = New Size(52, 21) Then
picDestroyer.Top = button.top
End If
If picDestroyer.Bounds.IntersectsWith(button.bounds) And picDestroyer.Size = New Size(21, 52) Then
picDestroyer.Left = button.left
End If
Next

要获取要捕捉到的按钮,您可以暂时禁用PictureBox,并在窗体上调用GetChildAtPoint()以获取PictureBox位置的子控件,指定GetChildAtPointSkip.Disabled作为第二个参数,这样搜索就不会包括您禁用的PictureBox,而是包括它上面/下面的所有其他控件。

之后,您可以将PictureBox的坐标设置为按钮的坐标。

还有一些可以对代码进行的改进:

  • 您可以在放下图片框后立即使用MouseUp事件而不是MouseLeave来更新图片框的位置。

  • 捕捉时设置图片框的X和Y坐标(而不仅仅是其中之一Left = X, Top = Y),以便在按钮的左上角正确捕捉。

  • 在循环中,通过Me.Controls.OfType(Of Button)()而不是仅通过Me.Controls进行迭代,因为OfType(Of TResult)将仅获得特定类型的控件(在本例中为Buttons)。例如,仅通过Me.Controls进行迭代将包括PictureBox本身,这可能会在未来引发问题(也许这就是您每次将其BackColor设置为Control的原因?)。

尽管如此,这个代码应该可以工作:

If e.Button = Windows.Forms.MouseButtons.Left Then
picDestroyer.Enabled = False 'Temporarily disable the picture box so that it isn't included in the child search.
Dim ChildBelowDestroyer As Control = Me.GetChildAtPoint(picDestroyer.Location, GetChildAtPointSkip.Disabled) 'Look for any child control that isn't disabled.
picDestroyer.Enabled = True 'Re-enable the picture box.
If ChildBelowDestroyer Is Nothing OrElse _
ChildBelowDestroyer.GetType() IsNot GetType(Button) Then
Return 'Do not continue if no child was found below the picture box, or if the child is not a button.
End If
picDestroyer.Location = ChildBelowDestroyer.Location 'Snap the picture box to the button (sets the X- and Y-coordinates).
For Each button In Me.Controls.OfType(Of Button)() 'OfType() to only look for buttons.
If picDestroyer.Bounds.IntersectsWith(button.Bounds) Then
button.BackColor = Color.Red
picDestroyer.BackColor = SystemColors.Control
End If
Next
End If

另一件需要记住的事情是在If语句中使用AndAlso而不是And,使用OrElse来代替Or,因为AndAlsoOrElse短路。

相关内容

  • 没有找到相关文章

最新更新