简体   繁体   English

如何在VB.NET中画一条线

[英]How to draw a line in VB.NET

I am trying to draw a simple line with VB.NET.我想用 VB.NET 画一条简单的线。

My code is as below, however when I run the code, only the form is shown up!我的代码如下,但是当我运行代码时,只显示表单! There is no line.没有线。

What did I do wrong here?我在这里做错了什么?

Public Class Form1
  Dim pen As System.Drawing.Graphics
  Private Sub Form1_Load(ByVal sender As System.Object,
                         ByVal e As System.EventArgs) Handles MyBase.Load
    pen = Me.CreateGraphics()
    pen.DrawLine(Pens.Azure, 10, 10, 20, 20)
  End Sub       
End Class

Basically, what you did wrong was to use the CreateGraphics method.基本上,您做错的是使用CreateGraphics方法。

This is something that you rarely, if ever, need to do.这是您很少(如果有的话)需要做的事情。 It's not as if the method is broken, of course.当然,这并不是说方法被破坏了。 It does exactly what it says: it is documented as doing: returning a Graphics object representing the drawing surface of your form.它完全按照它所说的做:它被记录为这样做:返回一个表示表单绘图表面的Graphics对象。

The problem is that whenever your form gets redrawn (which can happen for lots of reasons), the Graphics object basically gets reset .问题是,每当您的表单被重绘(这可能有很多原因), Graphics对象基本上会被重置 As a result, everything that you drew into the one that you obtained is erased.结果,你画进你得到的所有东西都被抹掉了。

A form is always redrawn when it is first loaded, so using CreateGraphics never makes sense in the Load event handler method.表单总是重绘,当它第一次加载,所以使用CreateGraphics永远是有道理的Load事件处理方法。 It is also going to be redrawn any time that it is minimized and restored, covered up by another window, or even resized (some of these depend on your operating system, graphics drivers, and your form's properties, but that's beyond the point) .它也将在任何时候被最小化和恢复,被另一个窗口覆盖,甚至调整大小(其中一些取决于您的操作系统、图形驱动程序和表单的属性,但这超出了重点)被重绘。

The only time that you might use CreateGraphics is when you want to show immediate feedback to the user that should not persist across redraws.您可能会使用CreateGraphics的唯一时间是当您希望向用户显示不应在重绘期间持续存在的即时反馈时。 For example, in the handler for the MouseMove event, when showing feedback for a drag-and-drop.例如,在MouseMove事件的处理程序中,当显示拖放反馈时。

So, what is the solution?那么,解决方案是什么? Always do your drawing inside of the Paint event handler method.始终在Paint事件处理程序方法内进行Paint That way, it persists across redraws, since a "redraw" basically involves raising the Paint event.这样,它会在重绘中持续存在,因为“重绘”基本上涉及引发Paint事件。

When the Paint event is raised, the handler is passed an instance of the PaintEventArgs class, which contains a Graphics object that you can draw into.当引发Paint事件时,处理程序会传递一个PaintEventArgs类的实例,该类包含一个可以绘制的Graphics对象。

So here's what your code should look like:所以你的代码应该是这样的:

Public Class Form1

    Protected Overridable Sub OnPaint(e As PaintEventArgs)
        ' Call the base class
        MyBase.OnPaint(e)

        ' Do your painting
        e.Graphics.DrawLine(Pens.Azure, 10, 10, 20, 20)
    End Sub

End Class

(Note also that in the above code, I am overriding the OnPaint method, rather than handling the corresponding Paint event. This is considered best practice for handling events in a derived class. But either way will work.) (还要注意,在上面的代码中,我覆盖了OnPaint方法,而不是处理相应的Paint事件。这被认为是处理派生类中事件的最佳实践。但无论哪种方式都可以。)

You should do this to draw your line你应该这样做来画你的线

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim myPen As Pen

   'instantiate a new pen object using the color structure
    myPen = New Pen(Color=Color.Blue, Width=2)

   'draw the line on the form using the pen object
   e.Graphics.DrawLine(pen=myPen, x1=100, y1=150, x2=150, y2=100)

   End Sub       
End Class

or there is more simple solution is just add this code in the Form Paint Event或者有更简单的解决方案,只需在 Form Paint Event 中添加此代码

e.Graphics.DrawLine(Pens.Azure, 10, 10, 20, 20)

You should put this code in the Paint event of the form, what's happening here is that the line is being drawn, but the form is re-painting as it finishes loading, so your line disappears.您应该将此代码放在表单的Paint事件中,这里发生的是正在绘制线条,但表单在完成加载时正在重新绘制,因此您的线条消失了。 Also, try a black or more contrasting color, or you'll miss it against the window background color of the form.此外,尝试使用黑色或对比色更强的颜色,否则您会在窗体的窗口背景色中错过它。

You can achieve this by adding a groupbox control to the form.您可以通过向表单添加一个 groupbox 控件来实现这一点。 Then remove the text (keep blank text), set the Height to 1 and select the BackColor you want.然后删除文本(保留空白文本),将高度设置为 1 并选择所需的背景颜色。

绘制多条线 Dim blackPen As New Pen(Color.Red, 3) Dim hwnd As IntPtr = PictureBox1.Handle Dim myGraphics As Graphics myGraphics = Graphics.FromHwnd(hwnd) Dim x1 As Integer = 100 Dim x2 As Integer = 500 Dim y1 As Integer = 10 Dim y2 As Integer = y1 Dim i As Int16 = 10, bothgap As Int16 = 20 ' myGraphics.DrawLine(blackPen, x1, y1, x2, y2) For i = 1 to 10 myGraphics.DrawLine(blackPen, x1, y1 + i * bothgap, x2, y1 + i * bothgap) 'myGraphics.DrawLine(blackPen, x1, y1 + 2 * 20, x2, y1 + 2 * 20) 'myGraphics.DrawLine(blackPen, x1, y1 + 3 * 20, x2, y1 + 3 * 20) 接下来 x1 = 100 x2 = 100 y1 = 10 + bothgap y2 = 200 + bothgap / 2 blackPen = New Pen(Color.Blue, 3) For i = 1 To 21 ' myGraphics.DrawLine (blackPen, x1, y1, x2, y2) myGraphics.DrawLine(blackPen, x1 + (i - 1) * bothgap, y1, x2 + (i - 1) * bothgap, y2) ' myGraphics.DrawLine(blackPen, x1 + 2 * bothgap, y1, x2 + 2 * bothgap, y2) 下一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM