鼠标移动事件的选择框

时间:2017-03-16 23:38:22

标签: vb.net gdi+

我正在尝试在目标上标记的“镜头”周围绘制一个选择矩形。我试图让它在“射击”的“范围”内射击,但它似乎随机出现。下面是我想要的图像(鼠标悬停在镜头周围的红色选择矩形)以及我的代码。

Red Rectangle is the "selection" rectangle

 Dim r As Rectangle

Dim shotList As New List(Of Point)
Dim scaleList As New List(Of Point)
Dim ShotCount As New List(Of Point)
Dim shotListBounds As New List(Of Rectangle)



Private Sub DrawShotHover(g As Graphics, location As Point, ByVal radius As Integer)
    Dim pn As New Pen(Brushes.Red, 2)
    Dim dashValues As Single() = {2, 1, 2, 1}
    pn.DashPattern = dashValues
    g.DrawRectangle(pn, New Rectangle(location.X - radius, location.Y - radius, radius * 2, radius * 2))

End Sub

 Private Sub mPictureBox_MouseClick(sender As Object, e As MouseEventArgs) Handles mPictureBox.MouseClick
    If e.Button = MouseButtons.Left Then

             If shotFlag = True Then

            Dim shot As New Point(e.X, e.Y)
            shotList.Add(shot)

            r = New Rectangle(e.X, e.Y, e.X + cboCaliber.EditValue / pLineDist() / 2, e.Y + cboCaliber.EditValue / pLineDist() / 2)
            shotListBounds.Add(r)

            shotDist = ShotDistance(shot)
            mPictureBox.Invalidate()

        End If
End Sub

 Private Sub mPictureBox_Paint(sender As Object, e As PaintEventArgs) Handles mPictureBox.Paint


    If LineScaleVal > 0 Then
        'SELECTION RECT
        If shotHoverSel = True Then
            DrawShotHover(e.Graphics, r.Location, cboCaliber.EditValue / pLineDist() / 2)
        End If

        'Point of Aim FLAG
        Dim _poa As New POA(e.Graphics, New Point(_poaX, _poaY), cboCaliber.EditValue / pLineDist() / 2)

        'SHOT FLAG
        For Each b As Point In shotList
            Dim _Bullet As New Bullet(e.Graphics, b, cboCaliber.EditValue / pLineDist() / 2, n)
        Next 
End Sub


 Private Sub mPictureBox_MouseMove(sender As Object, e As MouseEventArgs) Handles mPictureBox.MouseMove
   If shotListBounds.Count > 0 Then
        For Each r As Rectangle In shotListBounds
            Dim rad As Decimal = cboCaliber.EditValue / pLineDist()
            If e.X >= r.X AndAlso e.X <= r.X + rad AndAlso e.Y >= r.Y AndAlso e.Y <= r.Y + rad Then
                shotHoverSel = True
                selShot = r.Location
            End If
        Next
    End If
End Sub

 Private Sub mPictureBox_MouseEnter(sender As Object, e As EventArgs) Handles mPictureBox.MouseEnter
    If shotHoverSel = True Then
        mPictureBox.Invalidate()
    End If
End Sub



 Private Sub mPictureBox_MouseLeave(sender As Object, e As EventArgs) Handles mPictureBox.MouseLeave
    If shotHoverSel = False Then
        mPictureBox.Invalidate()
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

实际上有一个名为MouseEnter的事件。创建一个处理mPictureBox.MouseEnter

的方法
相关问题