方法和属性
来源:百度文库 编辑:神马文学网 时间:2024/04/29 23:11:05
在 Visual Basic 6.0 中,可以使用多种图形方法和属性在 Form 或 PictureBox 控件上绘制图形。Visual Basic 6.0 中的图形是以 Windows 图形设备接口 (GDI) API 为基础的。
在 Visual Basic 2005 中,图形由封装 GDI+ API 的
概念差异
在 Visual Basic 6.0 中,图形方法仅应用于 Form 对象和 PictureBox 控件。
在 Visual Basic 2005 中,图形方法可应用于窗体和支持
AutoRedraw 属性
在 Visual Basic 6.0 中,图形方法可从任何事件过程调用;AutoRedraw 属性用于当从 Paint 事件以外的事件调用图形方法时保持图形。
在 Visual Basic 2005 中,图形方法应仅从 Paint 事件过程调用,或对于一些所有者描述的控件,从各种 Draw 事件过程(
ClipControls 属性
在 Visual Basic 6.0 中,ClipControls 属性用于控制窗体或控件的绘制。设置为 True 时,仅重新绘制新公开的区域,这从理论上来说可提高性能。
Visual Basic 2005 中没有 ClipControls 属性的等效项;GDI+ 的性能增强和最新的视频适配器使该属性不再是必需的。
DrawMode 属性
在 Visual Basic 6.0 中,DrawMode 属性控制在一个图案上绘制另一个图案时图形对象的颜色。此属性仅影响单色或低分辨率显示器(256 色或更低)。
DrawMode 属性在 Visual Basic 2005 中没有等效项;当前的显示器不再需要此属性。
DrawStyle 属性
在 Visual Basic 6.0 中,DrawStyle 属性控制使用 Line 方法绘制的线的外观。如果 DrawWidth 属性设置为大于 1 的值,则 DrawStyle 属性不起作用,线将始终为实线。
在 Visual Basic 2005 中,通过设置 DrawLine 方法使用的 System.Drawing.Pen 类的
DrawWidth 属性
在 Visual Basic 6.0 中,DrawWidth 属性决定线的粗细(以像素为单位);DrawWidth 属性通常在执行图形方法前设置。
在 Visual Basic 2005 中,System.Drawing.Pen 控件的 Pen.Width 属性决定线的粗细;可以在创建 Pen 时作为参数设置 Width 属性,或在创建 Pen 后设置 Pen.Width。如果未指定 Pen.Width 属性,则默认值为 1 个像素。
Image 属性
在 Visual Basic 6.0 中,窗体或 PictureBox 控件的 Image 属性返回位图的句柄;该句柄可赋给 Picture 属性,或者作为一个值传递给 Windows API 调用。
在 Visual Basic 2005 中,位图不再具有句柄;实际位图本身作为
Line 方法
在 Visual Basic 6.0 中,通过指定左上角和右下角坐标以及可选参数 B
,可使用Line 方法来绘制矩形。FillColor 属性用于以某种纯色填充矩形,而 FillStyle 属性以交叉线图案填充矩形。
在 Visual Basic 2005 中,
Point 方法
在 Visual Basic 6.0 中,窗体或 PictureBox 控件的 Point 方法用于返回位于指定点的像素的颜色值。尽管 Point 方法可用于不包含图片的窗体或控件,但它最常用于从赋给 Picture 属性的某个位图中检索颜色。
在 Visual Basic 2005 中,Point 方法不再存在。可以使用 M:System.Drawing.Bitmap.GetPixel(System.Int32,System.Int32) 方法从位图检索颜色值。对于不包含图片的窗体或控件,可以查询
Print 方法
在 Visual Basic 6.0 中,Print 方法用于在窗体或 PictureBox 控件上显示文本。用于显示文本的字体由窗体或控件的 Font 属性决定,颜色由 ForeColor 属性决定。Print 方法不提供对文本位置的控制,只能水平显示文本。
在 Visual Basic 2005 中,使用 DrawString 方法显示文本。字体由
PSet 方法
在 Visual Basic 6.0 中,PSet 方法用于更改窗体或 PictureBox 控件上的像素颜色。如果 DrawWidth 属性设置为大于 1 的值,PSet 方法将绘制实心圆。如果使用了被省略的 ForeColor ,可通过另一个可选参数指定颜色。
在 Visual Basic 2005 中,没有 PSet 方法的等效项。若要更改窗体或 PictureBox 控件上单个像素的颜色,可使用 DrawEllipse 方法绘制一个高度和宽度都为 1 个像素的圆。若要复制 PSet 在 DrawWidth 大于 1 时的功能,请使用 FillEllipse 方法。
针对图形的代码更改
下面的代码示例演示 Visual Basic 6.0 和 Visual Basic 2005 在编码方法上的不同之处。
绘制普通线条
下面的代码演示如何在运行时在窗体上绘制线条。Visual Basic 6.0 示例使用 Line 方法;它以起始点和结束点的 X 和 Y 坐标以及颜色(可选)作为参数。Visual Basic 2005 示例使用 DrawLine 方法,该方法以
在 Visual Basic 6.0 中,默认度量单位是缇数;在 Visual Basic 2005 中,默认度量单位是像素。
' Visual Basic 6.0 Private Sub Form_Paint() ' Draw a solid black line 200 twips from the top of the form. Line (0, 200) - (ScaleWidth, 200), vbBlack End Sub
' Visual Basic 2005 Private Sub Form1_Paint(ByVal sender As Object, ByVal e _ As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint ' Draw a solid black line 25 pixels from the top of the form. e.Graphics.DrawLine(Pens.Black, 0, 25, Me.Width, 25) End Sub
绘制虚线
下面的代码演示如何在运行时在窗体上绘制虚线。在 Visual Basic 6.0 示例中,DrawStyle 属性决定线条的外观。Visual Basic 2005 示例使用
在 Visual Basic 6.0 中,默认度量单位是缇数;在 Visual Basic 2005 中,默认度量单位是像素。
' Visual Basic 6.0 Private Sub Form_Paint() ' Draw a dotted line 200 twips from the top of the form. Me.DrawStyle = vbDot Line (0, 200) - (ScaleWidth, 200), vbBlack End Sub
' Visual Basic 2005 Private Sub Form1_Paint1(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint ' Draw a dotted black line 25 pixels from the top of the form. Dim LPen As New System.Drawing.Pen(System.Drawing.Color.Black) LPen.DashStyle = Drawing2D.DashStyle.Dot e.Graphics.DrawLine(LPen, 0, 25, Me.Width, 25) End Sub
控制线条粗细
下面的代码演示如何在运行时在窗体上绘制不同粗细的线条。Visual Basic 6.0 示例使用 DrawWidth 属性。Visual Basic 2005 示例使用 Pens 对象的 Width 属性。
' Visual Basic 6.0 Private Sub Form_Paint() ' Draw a line with a thickness of 1 pixel. DrawWidth = 1 Line (0, 200)-(ScaleWidth, 200), vbBlack ' Draw a line with a thickness of 5 pixels. DrawWidth = 5 Line (0, 400)-(ScaleWidth, 400), vbBlack ' Draw a line with a thickness of 10 pixels. DrawWidth = 10 Line (0, 600)-(ScaleWidth, 600), vbBlack End Sub
' Visual Basic 2005 Private Sub Form1_Paint2(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint ' Draw a line with a thickness of 1 pixel. Dim TPen As New System.Drawing.Pen(System.Drawing.Color.Black, 1) e.Graphics.DrawLine(TPen, 0, 25, Me.Width, 25) ' Draw a line with a thickness of 5 pixels. TPen.Width = 5 e.Graphics.DrawLine(TPen, 0, 50, Me.Width, 50) ' Draw a line with a thickness of 10 pixels. TPen.Width = 10 e.Graphics.DrawLine(TPen, 0, 75, Me.Width, 75) End Sub
绘制圆
下面的代码演示如何在运行时在窗体上绘制圆。在 Visual Basic 6.0 示例中使用 Circle 方法;它以中心点的 X 和 Y 坐标、半径以及颜色(可选)作为参数。Visual Basic 2005 示例使用 DrawEllipse 方法,该方法以 Pen 对象、边界矩形左上角的 X 和 Y 坐标以及宽度和高度作为参数。
在 Visual Basic 6.0 中,默认度量单位是缇数;在 Visual Basic 2005 中,默认度量单位是像素。
' Visual Basic 6.0 Private Sub Form_Paint() ' Draw a 1000 twip diameter red circle Circle (500, 500), 500, vbRed End Sub
' Visual Basic 2005 Private Sub Form1_Paint3(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint ' Draw a 70 pixel diameter red circle. e.Graphics.DrawEllipse(Pens.Red, 0, 0, 70, 70) End Sub
绘制实心矩形
下面的代码演示如何在运行时在窗体上绘制两个矩形,一个以纯色填充,另一个以交叉线图案填充。在 Visual Basic 6.0 示例中,FillColor 和 FillStyle 属性与 Line 方法一起使用。使用 B 参数调用 Line 方法绘制矩形。
Visual Basic 2005 示例使用 Graphics.Rectangle 方法绘制轮廓,以及以 Brush 对象作为参数的 Graphics.FillRectangle 方法。本示例使用 SolidBrush 和 HatchBrush 两个控件。
在 Visual Basic 6.0 中,默认度量单位是缇数;在 Visual Basic 2005 中,默认度量单位是像素。
' Visual Basic 6.0 Private Sub Form_Paint() ' Draw a solid red rectangle. FillColor = vbRed FillStyle = vbSolid Line (10, 10)- (1000, 500), vbRed, B ' Draw a rectangle filled with a crosshatch pattern. FillColor = vbBlack FillStyle = vbCross Line (10, 500)- (1000, 1000), vbBlack, B End Sub
' Visual Basic 2005 Private Sub Form1_Paint4(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint ' Draw a solid red rectangle. Dim SBrush As New System.Drawing.SolidBrush _ (System.Drawing.Color.Red) e.Graphics.DrawRectangle(Pens.Red, 2, 2, 70, 40) e.Graphics.FillRectangle(SBrush, 2, 2, 70, 40) ' Draw a rectangle filled with a crosshatch pattern. Dim HBrush As New System.Drawing.Drawing2D.HatchBrush( _ System.Drawing.Drawing2D.HatchStyle.Cross, _ System.Drawing.Color.Black, System.Drawing.Color.Transparent) e.Graphics.DrawRectangle(Pens.Black, 2, 40, 70, 40) e.Graphics.FillRectangle(HBrush, 2, 40, 70, 40) End Sub
在窗体上显示图像
下面的代码演示运行时在窗体上显示图像的图形方法。Visual Basic 6.0 示例使用 PaintPicture 方法。Visual Basic 2005 示例使用 DrawImage 方法。
' Visual Basic 6.0 Private Sub Form_Paint() ' Create a stdPicture object. Dim Pict1 As New stdPicture Pict1 = LoadPicture("C:\Windows\Greenstone.bmp") PaintPicture Pict1, 0, 0 End Sub
' Visual Basic 2005 Private Sub Form1_Paint5(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint ' Create a Bitmap object. Dim Pict1 As New Bitmap("C:\Windows\Greenstone.bmp") e.Graphics.DrawImage(Pict1, 0, 0) End Sub
在窗体上显示文本
下面的代码演示运行时在窗体上显示文本字符串的图形方法。Visual Basic 6.0 示例使用 Print 方法。Visual Basic 2005 示例使用 DrawString 方法。
' Visual Basic 6.0 Private Sub Form_Paint() Me.Font.Size = 24 Me.Font.Bold = True Me.ForeColor = vbRed Print "Hello World!" End Sub
' Visual Basic 2005 Private Sub Form1_Paint6(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim TextFont As New System.Drawing.Font("Arial", 24, FontStyle.Bold) Dim TextBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red) e.Graphics.DrawString("Hello World!", TextFont, TextBrush, 10, 10) TextFont.Dispose() TextBrush.Dispose() End Sub
决定字符串的高度和宽度
下面的代码演示运行时决定窗体上的字符串大小,然后在周围绘制矩形的图形方法。Visual Basic 6.0 示例使用 TextHeight 和 TextWidth 方法。Visual Basic 2005 示例使用
' Visual Basic 6.0 Private Sub Form_Paint() Me.Font.Size = 24 Me.Font.Bold = True Me.ForeColor = vbRed Print "Hello World!" Line (0, 0)-(TextWidth("Hello World!"), _ TextHeight("Hello World!")), vbBlack, B End Sub
' Visual Basic 2005 Private Sub Form1_Paint7(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim TextFont As New System.Drawing.Font("Arial", 24, FontStyle.Bold) Dim TextBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red) e.Graphics.DrawString("Hello World!", TextFont, TextBrush, 10, 10) Dim TextSize As New System.Drawing.SizeF TextSize = e.Graphics.MeasureString("Hello World!", TextFont) e.Graphics.DrawRectangle(Pens.Black, 10, 10, TextSize.Width, TextSize.Height) TextFont.Dispose() TextBrush.Dispose() End Sub
绘制单个像素
下面的示例演示运行时更改窗体上单个像素颜色的图形方法。Visual Basic 6.0 示例使用 PSet 方法。Visual Basic 2005 示例使用 DrawEllipse 方法,其 Height 和 Width 参数均设置为 1
。
注意 在 Visual Basic 6.0 中,默认度量单位是缇数;在 Visual Basic 2005 中,默认度量单位是像素。
' Visual Basic 6.0 Private Sub Form_Paint() Me.DrawWidth = 1 PSet (1000, 1000), vbRed End Sub
' Visual Basic 2005 Private Sub Form1_Paint8(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint e.Graphics.DrawEllipse(Pens.Red, 70, 70, 1, 1) End Sub
决定单个像素的颜色
下面的代码演示在运行时确定窗体上图像中指定位置像素的颜色,然后以该颜色绘制矩形的图形方法。Visual Basic 6.0 示例使用 Point 方法检索颜色值。Visual Basic 2005 示例使用
在 Visual Basic 6.0 中,默认度量单位是缇数;在 Visual Basic 2005 中,默认度量单位是像素。
' Visual Basic 6.0 Private Sub Form_Paint() Dim PixelColor As Long Picture1.Picture = LoadPicture("C:\Windows\Greenstone.bmp") PixelColor = Picture1.Point(10, 10) FillColor = PixelColor Line (0, 0)-(100, 500), PixelColor, B End Sub
' Visual Basic 2005 Private Sub Form1_Paint9(ByVal sender As Object, ByVal e As _ System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim Pict1 As New Bitmap("C:\Windows\Greenstone.bmp") Picture1.Image = Pict1 Dim PixelColor As Color = Pict1.GetPixel(4, 4) Dim PixelBrush As New SolidBrush(PixelColor) e.Graphics.FillRectangle(PixelBrush, 0, 0, 100, 100) End Sub
图形属性和方法等效项
下表列出了 Visual Basic 6.0 图形属性和方法及它们在 Visual Basic 2005 中的等效项。
AutoRedraw 属性
新的实现。若要永久保存图形,请在 Paint 事件中放入图形方法。
Circle 方法
DrawEllipse 方法
ClipControls 属性
新的实现。ClipControls 属性不再是必需的。
Cls 方法
Clear 方法
CurrentX 属性
各种图形方法的 x 参数。例如,
CurrentY 属性
各种图形方法的 y 参数。例如,DrawRectangle (pen, x, y, width, height)
DrawMode 属性
新的实现。DrawMode 属性不再是必需的。
DrawStyle 属性
DashStyle 属性
DrawWidth 属性
FillColor 属性
SolidBrush 对象
FillStyle 属性
HatchBrush 对象
HasDC 属性
新的实现。GDI+ 不再需要设备上下文。
HDC 属性
新的实现。GDI+ 不再需要设备上下文。
Image 属性
新的实现。
Line 方法
DrawLine 方法
PaintPicture 方法
DrawImage 方法
Point 方法
无直接等效项。对于位图,请使用 Bitmap.GetPixel。对于窗体或控件,请使用
Print 方法
DrawString 方法
Pset 方法
DrawEllipse、FillEllipse 方法
TextHeight、TextWidth 属性
MeasureString 方法
升级说明
应用程序从 Visual Basic 6.0 升级到 Visual Basic 2005 时,图形方法并不升级,并会在代码中插入警告。由于 GDI 和 GDI+ 之间的巨大差异,现有的所有图形代码都需要重写。