如何在VB.NET中画一条线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17895619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:22:32  来源:igfitidea点击:

How to draw a line in VB.NET

.netvb.netwinformspen

提问by N.T.C

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

回答by Cody Gray

Basically, what you did wrong was to use the CreateGraphicsmethod.

基本上,您做错的是使用该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 Graphicsobject 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 Graphicsobject basically gets reset. As a result, everything that you drew into the one that you obtained is erased.

问题是,每当您的表单被重绘(这可能由于很多原因发生),Graphics对象基本上会被重置。结果,你画进你得到的所有东西都被抹掉了。

A form is alwaysredrawn when it is first loaded, so using CreateGraphicsnevermakes sense in the Loadevent handler method. 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).

表单总是在第一次加载时重绘,因此在事件处理程序方法中使用CreateGraphics永远没有意义Load。它也将在任何时候被最小化和恢复,被另一个窗口覆盖,甚至调整大小(其中一些取决于您的操作系统、图形驱动程序和表单的属性,但这超出了重点)被重绘。

The only time that you might use CreateGraphicsis when you want to show immediatefeedback to the user that should notpersist across redraws. For example, in the handler for the MouseMoveevent, when showing feedback for a drag-and-drop.

您可能会使用的唯一时间CreateGraphics是当您想要向用户显示不应在重绘期间持续存在的即时反馈时。例如,在事件的处理程序中,当显示拖放反馈时。MouseMove

So, what is the solution? Always do your drawing inside of the Paintevent handler method.That way, it persists across redraws, since a "redraw" basically involves raising the Paintevent.

那么,解决方案是什么?始终在Paint事件处理程序方法内进行绘图。这样,它在重绘中持续存在,因为“重绘”基本上涉及引发Paint事件。

When the Paintevent is raised, the handler is passed an instance of the PaintEventArgsclass, which contains a Graphicsobject that you can draw into.

Paint引发事件,处理程序是通过实例PaintEventArgs类,它包含了一个Graphics对象,你可以画成。

So here's what your code shouldlook 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 OnPaintmethod, rather than handling the corresponding Paintevent. This is considered best practice for handling events in a derived class. But either way will work.)

(还要注意,在上面的代码中,我覆盖了该OnPaint方法,而不是处理相应的Paint事件。这被认为是在派生类中处理事件的最佳实践。但无论哪种方式都可以。)

回答by YellowFlash

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)

回答by kprobst

You should put this code in the Paintevent 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. Also, try a black or more contrasting color, or you'll miss it against the window background color of the form.

您应该将此代码放在Paint表单的事件中,这里发生的是正在绘制线条,但是表单在完成加载时正在重新绘制,因此您的线条消失了。此外,尝试使用黑色或对比色更强的颜色,否则您会在窗体的窗口背景色中错过它。

回答by Shamz

You can achieve this by adding a groupbox control to the form. Then remove the text (keep blank text), set the Height to 1 and select the BackColor you want.

您可以通过向表单添加一个 groupbox 控件来实现这一点。然后删除文本(保留空白文本),将高度设置为 1 并选择所需的背景颜色。

回答by Gurbachan Singh

Draw multiple line 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) Next 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) Next

绘制多条线 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) 接下来