如何在VB.NET中画一条线 [英] How to draw a line in VB.NET

查看:84
本文介绍了如何在VB.NET中画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用VB.NET画一条简单的线.

I am trying to draw a simple line with VB.NET.

我的代码如下,但是当我运行代码时,仅显示表单!没有行.

My code is as below, however when I run the code, only the form is shown up! There is no line.

我在这里做错了什么?

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

推荐答案

基本上,您做错的是使用 CreateGraphics 方法.

Basically, what you did wrong was to use the CreateGraphics method.

这是您很少需要做的事情.当然,这并不是好像方法被破坏了.它完全按照其说的去做:被记录为正在做的事情:返回代表表单绘图表面的 Graphics 对象.

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 对象就基本上会得到 reset .结果,您提取到的所有内容都会被删除.

The problem is that whenever your form gets redrawn (which can happen for lots of reasons), the Graphics object basically gets reset. As a result, everything that you drew into the one that you obtained is erased.

表单在第一次加载时总是被重绘,因此在 Load 中使用 CreateGraphics 从不是有意义的>事件处理程序方法.只要将其最小化和还原,被另一个窗口覆盖,甚至调整的大小,它也将随时重新绘制(其中一些取决于您的操作系统,图形驱动程序和窗体的属性,但这超出了范围).点).

A form is always redrawn when it is first loaded, so using CreateGraphics never makes sense in the Load event 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 的情况是,您想向用户显示不应在整个重绘中持续存在的即时反馈.例如,在 MouseMove 事件的处理程序中,当显示拖放的反馈时.

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. For example, in the handler for the MouseMove event, when showing feedback for a drag-and-drop.

那么,解决方案是什么?始终在 Paint 事件处理程序方法中进行绘制.这样,由于重新绘制本质上涉及提高 Paint 事件.

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

引发 Paint 事件时,将向处理程序传递 PaintEventArgs 类的实例,该类包含一个 Graphics 对象,您可以吸引进来.

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.

这就是您的代码应该的样子:

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

(还要注意,在上面的代码中,我重写了 OnPaint 方法,而不是处理相应的 Paint 事件.这被认为是处理以下事件的最佳实践派生类.但是任何一种方法都可以.)

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

这篇关于如何在VB.NET中画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆