2014-07-23 37 views
0

我是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

我不认为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