【问题标题】:button to draw line vb.net not working?画线的按钮 vb.net 不起作用?
【发布时间】:2013-04-22 20:14:52
【问题描述】:

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

如果我使用下面的代码,

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 通过单击按钮绘制一条线???

【问题讨论】:

    标签: vb.net button line draw


    【解决方案1】:

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

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

    Dim g As Graphics = Me.CreateGraphics
    

    绘制图形比这更复杂,因为如果你绘制的内容包含在 e.ClipRectangle 属性中PaintEventArgs,但是上面的代码行现在应该可以帮助您开始。

    【讨论】:

      【解决方案2】:

      您使用的两个事件具有不同的 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
      

      【讨论】:

        猜你喜欢
        • 2014-01-18
        • 1970-01-01
        • 2016-03-21
        • 1970-01-01
        • 2012-10-30
        • 2016-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多