拖放后将PictureBox捕捉到按钮

时间:2017-01-23 03:27:21

标签: vb.net drag-and-drop alignment snapping

我正在制作战舰游戏,我正在使用按钮作为网格(播放器)。我正在使用一个图片框作为船,我正在努力使图片框捕捉到它碰撞的按钮。

我已经完成了拖放和碰撞部分,但我正在努力捕捉到按钮部分。图片框是两个按钮的大小。我试图通过使用picturebox.left = button.left将图片框与按钮对齐,但它选择了两个错误的按钮。

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

1 个答案:

答案 0 :(得分:0)

要获取要与之对齐的按钮,您可以暂时停用UIView并在表单上调用GetChildAtPoint(),以便在PictureBox的位置获取子控件,指定GetChildAtPointSkip.Disabled作为第二个参数,以便搜索不会包含您的已停用PictureBox,但会包含其上方/下方的所有其他控件。

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

您的代码也可以进行一些改进:

  • 您可以使用MouseUp event代替PictureBox更新图片框的位置。

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

  • 在循环中迭代Left = X, Top = Y而非Me.Controls.OfType(Of Button)(),因为OfType(Of TResult)只会 某种类型的控件(在此案例Me.Controls s)。仅对Button进行迭代将包含Me.Controls本身,这可能导致将来出现问题(也许这就是您每次都将PictureBox设置为BackColor的原因?)

所有这一切,这段代码应该有效:

Control

要记住的另一件事是使用AndAlso代替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 OrElse代替And Or - If以来{{1} }和AndAlsoshort-circuited