2D绘图:按钮——重绘之谜

发布于 2025-01-06 21:00:13 字数 1744 浏览 1 评论 0原文

我有一个有趣的任务,即使用 VB.NET 绘制一些图表。到目前为止,我读到的有关 GDI+ 和 e.graphics 的所有内容都非常奇怪。我想做的就是

1) 单击按钮 1 计算一些坐标

2) 单击按钮 2 用按钮 1 中的数字绘制一条线

3) 单击按钮 1 以获取新坐标

4) 单击按钮 2 以绘制上一条线和新线。

5) 单击按钮 3 清除图表。

因此,我决定在名为 panel1 的面板上绘制所有内容。我有一个在屏幕上绘制的例程,称为绘制线,

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()?

I have the interesting task of doing some graphs using VB.NET. So far, everything that I´ve been reading about GDI+ and e.graphics whatever is really weird. All I want to do is

1) Calculate some coordinates clicking button 1

2) Click button 2 to draw a line with the numbers from button 1

3) Click button 1 to get new coordinates

4) click button 2 to draw the previous line AND the new line.

5) click button 3 to clear the graph.

So I decided to draw everthing on top of a Panel, called panel1. I have a routine that draws on screen called 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

and the other routines:

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

So far, only using the GraphicsHandler thing is working, but everytime I try to minimize the window or draw a new line, the previous lines are erased. Can some kind soul explain to me proper way to do the simple 1-5 above? For example, how can I call drawlines()from the button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2025-01-13 21:00:13

.NET 使用 WinForms 和 GDI+ 实现了全新的图形操作模型。在这个美丽的新世界中,您旧的 VB6 技能将无法为您提供帮助。

因此,首先要放弃 GraphicsHandler。您的所有绘画都应通过面板的 Paint 事件完成。

您应该将每一行存储在表单级别的数组或 List(Of Point) 中。然后每次调用 Paint 事件时,您都会再次绘制所有线条。与 VB6 控件不同,.NET 控件不会记住从一个 Paint 事件到下一个 Paint 事件的图形状态。

如果您需要在按钮 Click 事件结束时强制重绘,您可以调用 Panel.Invalidate()

PSEUDOCODE:

Private myCoordinates As List(Of Point) = New List(Of Point)

Sub Button_click(sender, e)
  '' Store new coordinate
  myCoordinates.Add(New Point(x, y))
  myCoordinates.Add(New Point(x, y))
  myPanel.Invalidate()
End Sub


Sub Panel_Paint(sender, e) Handles myPanel.Paint
  For tIndex As Int32 = 0 To myCoordinates.Count - 1 Step 2
    e.graphics.DrawLine(myCoordinate(tIndex), myCoordinates(tIndex+1))
  Next
End Sub

.NET implements a completely new graphics manipulation model with WinForms and GDI+. Your old VB6 skills will not serve you well in this brave new world.

So start by ditching the GraphicsHandler. ALL your painting should be done via the Panel's Paint event.

You should store each line in an array or List(Of Point) at the form level. Then each time the Paint event is called you draw all your lines again. .NET controls DO NOT REMEMBER their graphical state from one Paint event to the next, unlike VB6 controls.

If you need to force a redraw at the end of your button Click event you would call your Panel.Invalidate()

PSEUDOCODE:

Private myCoordinates As List(Of Point) = New List(Of Point)

Sub Button_click(sender, e)
  '' Store new coordinate
  myCoordinates.Add(New Point(x, y))
  myCoordinates.Add(New Point(x, y))
  myPanel.Invalidate()
End Sub


Sub Panel_Paint(sender, e) Handles myPanel.Paint
  For tIndex As Int32 = 0 To myCoordinates.Count - 1 Step 2
    e.graphics.DrawLine(myCoordinate(tIndex), myCoordinates(tIndex+1))
  Next
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文