我不认为GDI +给你这样的线对象,但你可以创建一个图形路径对象的集合,可以让你做你想做的。你当然可以检测点击事件(如下所示)。至于改变线的可见属性,我相信你将不得不调用Paint方法并使用具有不同属性的Pen对象。
在这个例子中,DrawLines方法为每一行创建一个GraphicsPath,然后调用PictureBox_Paint来更新屏幕。您可以更改MyPen的颜色或宽度,然后再次调用paint方法重新绘制线条。
PictureBox1_MouseDown方法使用IsOutlineVisible来确定是否单击某个路径。
'Collection of paths to hold references to the "Lines" and a pen for drawing them
Private MyPaths As New List(Of Drawing2D.GraphicsPath)
Private MyPen As New Pen(Color.Red, 4)
Private Sub DrawLines()
'Loop over the lines and add a path for each to the collection
For Each Ln As Line In Lines
Dim MyPath As New Drawing2D.GraphicsPath()
MyPath.AddLine(Ln.Start, Ln.End)
MyPaths.Add(MyPath)
Next
'Call method to draw the paths with the specified pen
PictureBox_Paint(Me, New System.Windows.Forms.PaintEventArgs(Me.PictureBox1.CreateGraphics, Me.PictureBox1.ClientRectangle))
End Sub
Public Sub PictureBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
For Each Pth As Drawing2D.GraphicsPath In MyPaths
e.Graphics.DrawPath(MyPen, Pth)
Next
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
'Create a Graphics object for PictureBox (needed for the IsOutlineVisible method)
Dim Gr As Graphics = Me.PictureBox1.CreateGraphics
'Loop over paths in collection and check if mouse click was on top of one
For Each Pth As Drawing2D.GraphicsPath In MyPaths
If Pth.IsOutlineVisible(e.Location.X, e.Location.Y, MyPen, Gr) Then
MessageBox.Show("Path was clicked", "Click")
End If
Next
End Sub