1

我正在尝试使用不起作用的按钮创建一条线。

如果我使用以下代码,

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
      Dim g As Graphics = e.Graphics
      Dim pn As New Pen(Color.Blue)
      Dim pt1 As New Point(30, 30)
      Dim pt2 As New Point(110, 100)
      g.DrawLine(pn, pt1, pt2)
End sub

它工作得很好,但是如果我只想在单击按钮后进行绘制,例如,

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      Dim g As Graphics = e.Graphics
      Dim pn As New Pen(Color.Blue)
      Dim pt1 As New Point(30, 30)
      Dim pt2 As New Point(110, 100)
      g.DrawLine(pn, pt1, pt2)

    End Sub

它说“'Graphics' 不是 'System.EventArgs' 的成员。???

我也尝试改变:

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

至:

   Private Sub Button1_Click(ByVal sender As graphics.Object, ByVal e As System.EventArgs) Handles Button1.Click

还有一些类似的变化(很多人都列出来了),但我得到了一些错误响应。

那么如何使用 e.graphics 通过单击按钮绘制一条线???

4

2 回答 2

0

您需要对表单的 Graphics 上下文的引用(我在这里假设为 WinForms)。

最简单的方法是在单击按钮的顶部添加这一行:

Dim g As Graphics = Me.CreateGraphics

绘制图形比这更复杂,因为OnPaint如果您绘制的内容包含在 的e.ClipRectangle属性中,您应该真正将绘制的内容持久保存到内存中并在事件触发时根据需要重新绘制它PaintEventArgs,但上面的代码行应该让您现在开始。

于 2013-04-22T12:09:21.593 回答
0

您使用的两个事件具有不同的 EventArgs。Click EventArgs 包含有关单击的信息,PaintEventArgs 包含有关要绘制什么的信息。

您可以调用以重新绘制按钮并使用您的 onPaint 方法。这是一种很好的编程风格。将事件与相应的内容配对。画来画。点击点击。

Private drawLine as boolean = false

Private Sub Button1_Paint(sender As Object, e As PaintEventArgs) Handles Button1.Paint
  If drawLine then
    Dim g As Graphics = e.Graphics
    Dim pn As New Pen(Color.Blue)
    Dim pt1 As New Point(30, 30)
    Dim pt2 As New Point(110, 100)
    g.DrawLine(pn, pt1, pt2)
  End if
End sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  drawLine = true
  Button1.refresh()
End Sub
于 2013-04-22T12:10:03.600 回答