【发布时间】:2012-02-22 02:46:09
【问题描述】:
我的有趣任务是使用 VB.NET 绘制一些图表。到目前为止,我一直在阅读的关于 GDI+ 和 e.graphics 的所有内容都非常奇怪。我想做的就是
1) 点击按钮 1 计算一些坐标
2) 点击按钮 2 用按钮 1 的数字画一条线
3) 点击按钮1获取新坐标
4) 单击按钮 2 绘制上一行和新行。
5) 单击按钮 3 清除图形。
所以我决定在一个名为 panel1 的 Panel 上绘制所有内容。我有一个在屏幕上绘制的例程,称为 drawlines,
Private Sub drawlines(ByVal g As Graphics, ByVal c As Color)
Dim p As New Pen(c, 1)
g.DrawLine(p, xStart, yStart, xEnd, yEnd)
p.Dispose()
End Sub
和其他例程:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
'AddHandler Panel1.Paint, AddressOf DrawLine
GraphicsHandler = Panel1.CreateGraphics
End Sub
Private Sub drawlines(ByVal g As Graphics, ByVal c As Color)
Dim p As New Pen(c, 1)
g.DrawLine(p, xStart, yStart, xEnd, yEnd)
p.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'GraphicsHandler = Panel1.CreateGraphics
GraphicsHandler.DrawLine(myPen, 10, 10, 200, 100)
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
xStart = CInt(Math.Ceiling(Rnd() * 200))
yStart = CInt(Math.Ceiling(Rnd() * 100))
xEnd = CInt(Math.Ceiling(Rnd() * 200))
yEnd = CInt(Math.Ceiling(Rnd() * 100))
Me.Panel1.Invalidate()
End Sub
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Panel1.Paint
drawlines(e.Graphics, Color.Blue)
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
GraphicsHandler.Clear(Color.White)
End Sub
到目前为止,仅使用 GraphicsHandler 的东西是有效的,但是每次我尝试最小化窗口或绘制新行时,以前的行都会被删除。某个善良的灵魂可以向我解释完成上述简单 1-5 的正确方法吗?例如,如何从按钮调用 drawlines()?
【问题讨论】:
标签: vb.net winforms vb6-migration