Shape 控件(针对 Visual Basic 6.0 用户)

更新:2007 年 11 月

Visual Basic 6.0 中的 Shape 控件在 Visual Basic 2008 中没有等效项。但是,您可以使用 Graphics 方法获得相同的结果,也可以使用 Visual Basic Power Pack OvalShape 或 RectangleShape 控件。这些控件可用作外接程序。

概念差异

在 Visual Basic 6.0 中,Shape 控件提供了一种简单的方法,用于在设计时绘制矩形、圆和其他形状。Shape 控件是一个“轻量”控件,即该控件没有 Windows 句柄,也称作 HWnd。

在 Visual Basic 2008 中,没有 Shape 控件的等效项,并且也不再支持轻量控件。但是提供了在设计时和运行时在窗体上绘制形状的方法。

说明:

Visual Basic Power Pack 控件包括 LineShape、OvalShape 和 RectangleShape 控件,这些控件可用于替换 Line 和 Shape 控件。除了拥有 Visual Basic 6.0 Line 和 Shape 控件的行为之外,这些控件还增加了新的功能,包括渐变填充、运行时选择以及运行时事件。

您可以从 MSDN 网站的 Microsoft Visual Basic 2005 Power Packs 页面下载 Visual Basic Power Pack。

设计时,可以通过添加 Label 控件并将 Text 属性设置为空字符串,以及将 BorderStyle 属性设置为 FixedSingle,将 BackColorWidthHeight 设置为所需颜色和尺寸,在窗体上绘制正方形或矩形。

运行时,可以通过从 Graphics 类创建新对象并调用其方法,在窗体的 Paint 事件处理程序中绘制矩形、椭圆和复杂的形状。

在 Visual Basic 6.0 中,可以通过向容器添加 Shape 控件,使用 Shape 控件在容器控件(例如 PictureBox 或 Frame 控件)上绘制形状。

在 Visual Basic 2008 中,可以通过在容器控件的 Paint 事件中调用 Graphics 方法获得相同的效果。

Shape 控件的代码更改

下面的代码示例演示 Visual Basic 6.0 和 Visual Basic 2008 在编码方法上的不同之处。

绘制矩形的代码更改

下面的代码演示如何在运行时在窗体上绘制实心矩形。在 Visual Basic 6.0 示例中,使用的是 Shape 控件,并假定在设计时添加了一个 Line 控件。Visual Basic 2008 示例演示了两种不同的方法:使用 Label 控件和使用 Graphics 方法。

说明:

在 Visual Basic 6.0 中,默认度量单位为缇;在 Visual Basic 2008 中,默认度量单位为像素。

' Visual Basic 6.0
Private Sub Form_Load()
    ' Show a solid red rectangle 200 twips from the top left.
    Shape1.Top = 200
    Shape1.Left = 200
    Shape1.FillColor = vbRed
    Shape1.FillColor= vbFSSolid
    Shape1.BorderColor = vbRed
End Sub
' Visual Basic
' Using a Label control.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
    Dim Shape1 As New System.Windows.Forms.Label
    ' Show a solid red rectangle 14 pixels from the top left.
    Shape1.Location = New System.Drawing.Point(14, 14)
    Shape1.Size = New System.Drawing.Size(200, 100)
    Shape1.BorderStyle = BorderStyle.None
    Shape1.BackColor = System.Drawing.Color.Red
    Shape1.Text = ""
    Controls.Add(Shape1)
End Sub
' Visual Basic
' Using Graphics methods.
Private Sub Form2_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    ' Draw a solid blue rectangle below the red rectangle.
    e.Graphics.FillRectangle(Brushes.Blue, 14, 120, 200, 100)
End Sub

绘制圆的代码更改

下面的代码演示如何在运行时在窗体上绘制圆。在 Visual Basic 6.0 示例中,使用的是 Shape 控件,并假定在设计时添加了一个 Shape 控件。此 Visual Basic 2008 示例使用的是 Graphics 方法。

说明:

在 Visual Basic 6.0 中,默认度量单位为缇;在 Visual Basic 2008 中,默认度量单位为像素。

' Visual Basic 6.0
Private Sub Form_Load()
    ' Draw a 1000 twip diameter red circle.
    Shape1.Top = 0
    Shape1.Left = 0
    Shape1.Height = 1000
    Shape1.Width = 1000
    Shape1.Shape = vbShapeCircle
    Shape1.BorderColor = vbRed
End Sub
' Visual Basic
    Private Sub Form3_Paint(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 应用程序时,引用 Shape 控件的代码也将升级以使用 Visual Basic Power Pack 库中包含的 OvalShape 或 RectangleShape 控件。如果在升级过程中发现存在对 Shape 控件的引用,将发出一个警告,您将必须安装此库并从升级后的项目中引用它。

您可以从 MSDN 网站的 Microsoft Visual Basic 2005 Power Packs 页面下载 Visual Basic Power Pack。

请参见

概念

图形(针对 Visual Basic 6.0 用户)