更改线属性

时间:2014-07-23 08:55:59

标签: vb.net 2d gdi+ graphic

我是GDI +的新手。有人可以帮我完成任务吗?

这是我的代码:

Private Sub DrawLines(ByVal g As Graphics)
    For Each line As Line In Me.lines
        g.DrawLine(Pens.White, line.Start, line.End)
    Next line
End Sub

该线条是在picturebox对象上绘制的。

画线是否会成为对象?如果是,如何启用某些事件,如点击或其他?如何更改该行的属性?

如果鼠标光标位于行区域,则显示此Image,该行可以将颜色更改为红色。

我的问题:我怎么能这样做?检测,检索线区域并更改线条的颜色

有人能给我一个简单的逻辑吗?

任何帮助都会受到赞赏。感谢

1 个答案:

答案 0 :(得分:1)

我不认为GDI +会为你提供这样的线条对象,但是你可以创建一个可以让你做你想做的GraphPath对象的集合。您当然可以检测到点击事件(如下所示)。至于更改一行的可见属性,我相信你必须调用一个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