如何找到哪个物体凌驾于他人之上

时间:2018-06-28 07:31:13

标签: vb.net class draw region

我有一个用于在以前制作的画布上绘制自定义形状的类(形状)。 为了检查鼠标指针是否在形状上,我使用了canvase_mousemove事件,并在循环中检查了shape.region.invisible(e.location)是否为真(如果为true则表示鼠标超出了形状)。 但是当两个或两个以上对象重叠时,“ shape.region.invisible(e.location)”为真,我找不到哪个是其他顶部的。

在形状类别中:

Private _shapeRegion As Region
Public ReadOnly Property Region() As Region
    Get
        Return _shapeRegion
    End Get
End Property

Public Sub DrawShape(ByVal point As PointF, ByVal size As SizeF, ByRef graphics As Graphics)
    _shapePath = New GraphicsPath
    _shapePath.AddEllipse(New RectangleF(point, size))
graphics.DrawPath(_basePen, _shapePath)
graphics.FillPath(_baseBrush, _shapePath)
_shapeRegion = New Region(_shapePath) 
End Sub

在画布上:

 Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
    MyBase.OnMouseMove(e)
    For Each _shape As Shape In _canvasShapes
    _shape.Highlight=_shape.Region.IsVisible(e.Location)
        _shape.ApplyProperties()
    Next
    Me.Invalidate()
End Sub

enter image description here

1 个答案:

答案 0 :(得分:1)

我假设您按照形状在CREATE TABLE new_event_data SELECT * from table1, table2 where table1.vehicle=table2.vehicle and table2.eventdate BETWEEN table1.start_date AND table1.finish_date 列表中的出现来绘制形状,因此首先绘制形状0,然后绘制形状1,依此类推。

因此索引较大的形状是顶部形状,您可以使用反向shapes循环来查找它:

For

我还建议在创建Dim selectedIndex = -1 For index = shapes.Count-1 To 0 Step -1 If(shapes(index).IsVisible(Point.Empty)) Then selectedIndex = index shapes(index).Highlight = True Exit For End If Next ' selectedIndex contains the selected index Using时使用GraphicsPath,例如,在创建Region时使用:

IsVisible