visual c++gdi_Visual Basic .NET中的GDI +图形

visual c++gdi

GDI+ is the way to draw shapes, fonts, images or generally anything graphic in Visual Basic .NET.

GDI +是在Visual Basic .NET中绘制形状,字体,图像或一般任何图形的方法。

This article is the first part of a complete introduction to using GDI+ in Visual Basic .NET.

本文是在Visual Basic .NET中使用GDI +的完整介绍的第一部分。

GDI+ is an unusual part of .NET. It was here before .NET (GDI+ was released with Windows XP) and it doesn't share the same update cycles as the .NET Framework. Microsoft's documentation usually states that Microsoft Windows GDI+ is an API for C/C++ programmers into the Windows OS. But GDI+ also includes the namespaces used in VB.NET for software-based graphics programming.

GDI +是.NET的不寻常部分。 .NET(与Windows XP一起发布的GDI +)就在这里,并且它没有与.NET Framework共享相同的更新周期。 Microsoft的文档通常指出Microsoft Windows GDI +是Windows OS中C / C ++程序员的API。 但是GDI + 包括VB.NET中用于基于软件的图形编程的名称空间

WPF ( WPF )

But it's not the only graphics software provided by Microsoft, especially since Framework 3.0. When Vista and 3.0 were introduced, the totally new WPF was introduced with it. WPF is a high-level, hardware accelerated approach to graphics. As Tim Cahill, Microsoft WPF software team member, puts it, with WPF "you describe your scene using high-level constructs, and we’ll worry about the rest." And the fact that it's hardware accelerated means that you don't have to drag down the operation of your PC processor drawing shapes on the screen. Much of the real work is done by your graphics card.

但这不是Microsoft提供的唯一图形软件,尤其是从Framework 3.0开始。 引入Vista和3.0时,引入了全新的WPF。 WPF是一种高级的,硬件加速的图形方法。 正如Microsoft WPF软件团队成员Tim Cahill所说,使用WPF,“您使用高级结构描述场景,其余的我们将担心。” 而且其硬件加速的事实意味着您不必拖动PC处理器在屏幕上绘制形状的操作。 大部分实际工作是由图形卡完成的。

We've been here before, however. Every "great leap forward" is usually accompanied by a few stumbles backward, and besides, it will take years for WPF to work its way through the zillions of bytes of GDI+ code. That's especially true since WPF just about assumes that you're working with a high-powered system with lots of memory and a hot graphics card. That's why many PCs couldn't run Vista (or at least, use the Vista "Aero" graphics) when it was first introduced. So this series continues to be available on the site for any and all who continue to need to use it.

但是,我们以前来过这里。 每个“大跃进”通常都伴随着一些倒退,此外,WPF要花费数十亿字节的GDI +代码来工作,将需要数年的时间。 这是特别正确的,因为WPF只是假设您正在使用具有大量内存和热图形卡的高性能系统。 这就是为什么许多PC首次推出时都无法运行Vista(或至少使用Vista的“ Aero”图形)的原因。 因此,该系列将继续在网站上提供,供任何仍需要使用它的所有人使用。

好的奥尔码 ( Good Ol' Code )

GDI+ isn't something that you can drag onto a form like other components in VB.NET. Instead, GDI+ objects generally have to be added the old way -- by coding them from scratch! (Although, VB .NET does include a number of very handy code snippets that can really help you.)

GDI +不能像VB.NET中的其他组件一样拖到窗体上。 相反,通常必须以旧的方式添加GDI +对象-通过从头开始编码! (尽管,VB .NET确实包含许多非常方便的代码片段,它们确实可以为您提供帮助。)

To code GDI+, you use objects and their members from a number of .NET namespaces. (At the present time, these are actually just wrapper code for Windows OS objects which actually do the work.)

要编码GDI +,您可以使用许多.NET名称空间中的对象及其成员。 (目前,这些实际上只是用于实际完成工作的Windows OS对象的包装代码。)

命名空间 ( Namespaces )

The namespaces in GDI+ are:

GDI +中的名称空间是:

System.Drawing

系统绘图

This is the core GDI+ namespace. It defines objects for basic rendering (fonts, pens, basic brushes, etc.) and the most important object: Graphics. We'll see more of this in just a few paragraphs.

这是核心 GDI +命名空间。 它定义了用于基本渲染的对象( 字体 ,笔,基本笔刷等)以及最重要的对象:图形。 我们将在短短几段中看到更多内容。

System.Drawing.Drawing2D

System.Drawing.Drawing2D

This gives you objects for more advanced two-dimensional vector graphics. Some of them are gradient brushes, pen caps, and geometric transforms.

这为您提供了用于更高级的二维矢量图形的对象。 其中一些是渐变笔刷,笔帽和几何变换。

System.Drawing.Imaging

系统绘图成像

If you want to change graphical images - that is, change the palette, extract image metadata, manipulate metafiles, and so forth - this is the one you need.

如果要更改图形图像(即,更改调色板,提取图像元数据,操作图元文件等),这就是您所需要的。

System.Drawing.Printing

系统绘图打印

To render images to the printed page, interact with the printer itself, and format the overall appearance of a print job, use the objects here.

要将图像渲染到打印的页面,与打印机本身进行交互并格式化打印作业的整体外观,请使用此处的对象。

System.Drawing.Text

System.Drawing.Text

You can use collections of fonts with this namespace.

您可以在此命名空间中使用字体集合。

图形对象 ( Graphics Object )

The place to start with GDI+ is the Graphics object. Although the things you draw show up on your monitor or a printer, the Graphics object is the "canvas" that you draw on.

从GDI +开始的地方是Graphics对象。 尽管您绘制的东西显示在监视器或打印机上,但是Graphics对象是您绘制的“画布”。

But the Graphics object is also one of the first sources of confusion when using GDI+. The Graphics object is always associated with a particular device context. So the first problem that virtually every new student of GDI+ confronts is, "How do I get a Graphics object?"

但是,使用GDI +时,Graphics对象还是引起混乱的最早来源之一。 Graphics对象始终与特定的设备上下文关联 。 因此,几乎每个GDI +新生都面临的第一个问题是:“我如何获得图形对象?”

There are basically two ways:

基本上有两种方法:

  1. You can use the e event parameter that is passed to the OnPaint event with the PaintEventArgs object. Several events pass the PaintEventArgs and you can use the to refer to the Graphics object that is already being used by the device context.

    您可以将e事件参数与PaintEventArgs对象一起使用,该参数传递给OnPaint事件。 几个事件传递了PaintEventArgs ,您可以使用来引用设备上下文已经使用的Graphics对象。

  2. You can use the CreateGraphics method for a device context to create a Graphics object.

    您可以对设备上下文使用CreateGraphics方法来创建Graphics对象。

Here's an example of the first method:

这是第一种方法的示例:

Protected Overrides Sub OnPaint( _
   ByVal e As System.Windows.Forms.PaintEventArgs)
   Dim g As Graphics = e.Graphics
   g.DrawString("About Visual Basic" & vbCrLf _
   & "and GDI+" & vbCrLf & "A Great Team", _
   New Font("Times New Roman", 20), _
   Brushes.Firebrick, 0, 0)
   MyBase.OnPaint(e)
End Sub

Click Here to display the illustration

点击这里显示插图

Add this into the Form1 class for a standard Windows Application to code it yourself.

将其添加到标准Windows应用程序的Form1类中以自己编写代码。

In this example, a Graphics object is already created for the form Form1. All your code has to do is create a local instance of that object and use it to draw on the same form. Notice that your code Overrides the OnPaint method. That's why MyBase.OnPaint(e) is executed at the end. You need to make sure that if the base object (the one you're overriding) is doing something else, it gets a chance to do it. Often, your code works without this, but it's a good idea.

在此示例中,已经为Form1表单创建了Graphics对象。 您所有的代码要做的就是创建该对象的本地实例,并使用它绘制相同的表单。 请注意,您的代码将覆盖 OnPaint方法。 这就是在最后执行MyBase.OnPaint(e)的原因。 您需要确保如果基础对象(您要覆盖的对象)正在执行其他操作,则有机会这样做。 通常,您的代码无需使用此代码即可工作,但这是一个好主意。

PaintEventArgs ( PaintEventArgs )

You can also get a Graphics object using the PaintEventArgs object handed to your code in the OnPaint and OnPaintBackground methods of a Form. The PrintPageEventArgs passed in a PrintPage event will contain a Graphics object for printing. It's even possible to get a Graphics object for some images. This can let you paint right on the image the same way you would paint on a Form or component.

您还可以使用传递给Form的OnPaintOnPaintBackground方法中的代码的PaintEventArgs对象获得Graphics对象。 在PrintPage事件传递的PrintPageEventArgs将包含一个图形对象进行打印。 甚至可以为某些图像获取Graphics对象。 这样一来,您就可以像在窗体或组件上一样在图像上正确绘画。

事件处理程序 ( Event Handler )

Another variation of method one is to add an event handler for the Paint event for the form. Here's what that code looks like:

方法一的另一种形式是为窗体的Paint事件添加一个事件处理程序。 该代码如下所示:

Private Sub Form1_Paint( _
   ByVal sender As Object, _
   ByVal e As System.Windows.Forms.PaintEventArgs) _
   Handles Me.Paint
   Dim g As Graphics = e.Graphics
   g.DrawString("About Visual Basic" & vbCrLf _
   & "and GDI+" & vbCrLf & "A Great Team", _
   New Font("Times New Roman", 20), _
   Brushes.Firebrick, 0, 0)
End Sub

创建图形 ( CreateGraphics )

The second method to get a Graphics object for your code uses a CreateGraphics method that is available with many components. The code looks like this:

为您的代码获取Graphics对象的第二种方法是使用CreateGraphics方法,该方法可用于许多组件。 代码如下:

Private Sub Button1_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles Button1.Click
   Dim g = Me.CreateGraphics
   g.DrawString("About Visual Basic" & vbCrLf _
   & "and GDI+" & vbCrLf & "A Great Team", _
   New Font("Times New Roman", 20), _
   Brushes.Firebrick, 0, 0)
End Sub

There are a couple of differences here. This is in the Button1.Click event because when Form1 repaints itself in the Load event, our graphics are lost. So we have to add them in a later event. If you code this, you'll notice that the graphics are lost when Form1 has to be redrawn. (Mimimize and maximize again to see this.) That's a big advantage to using the first method.

这里有几个区别。 这是在Button1.Click事件中,因为Form1Load事件中重新绘制自身时,我们的图形会丢失。 因此,我们必须在以后的事件中添加它们。 如果编写此代码,则您将注意到必须重新绘制Form1时图形丢失。 (最小化并再次最大化以查看这一点。)这是使用第一种方法的一大优势。

Most references recommend using the first method since your graphics will be repainted automatically. GDI+ can be tricky!

大多数参考文献建议使用第一种方法,因为您的图形将自动重新绘制。 GDI +可能很棘手!

翻译自: https://www.thoughtco.com/gdi-graphics-in-visual-basic-net-3424305

visual c++gdi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值