Shape 控件(针对 Visual Basic 6.0 用户)
来源:百度文库 编辑:神马文学网 时间:2024/05/01 04:02:12
Visual Basic 6.0 中的 Shape 控件在 Visual Basic 2005 中没有等效项。但是可以使用
概念差异
在 Visual Basic 6.0 中,Shape 控件提供了一种简单的方法,用于在设计时绘制矩形、圆和其他形状。Shape 控件是一个“轻量级”控件,这意味着它没有 Windows 句柄(也称为 HWnd)。
在 Visual Basic 2005 中,没有 Shape 控件的等效项,也不再支持轻量级控件。但是也提供了在设计时和运行时在窗体上绘制形状的方法。
设计时,可以通过添加
运行时,可以通过从 Graphics 类创建新对象并调用其方法,在窗体的
在 Visual Basic 6.0 中,可以通过向容器添加 Shape 控件,使用 Shape 控件在容器控件(例如 PictureBox 或 Frame 控件)上绘制形状。
在 Visual Basic 2005 中,可以通过调用容器控件的 Paint
事件的 Graphics 方法实现相同效果。
Shape 控件的代码更改
下面的代码示例演示 Visual Basic 6.0 和 Visual Basic 2005 在编码方法上的不同之处。
绘制矩形的代码更改
下面的代码演示如何在运行时在窗体上绘制实心矩形。在 Visual Basic 6.0 示例中使用 Shape 控件;其中假定 Line 控件是在设计时添加的。Visual Basic 2005 示例演示两种不同的方法,一种方法使用 Label 控件,另一种则使用 Graphics 方法。
在 Visual Basic 6.0 中,默认度量单位是缇;在 Visual Basic 2005 中,默认度量单位是像素。
' 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 2005 ' 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 2005 ' 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 2005 示例使用 Graphics 方法。
在 Visual Basic 6.0 中,默认度量单位是缇;在 Visual Basic 2005 中,默认度量单位是像素。
' 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 2005 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 应用程序升级到 Visual Basic 2005 后,任何 Shape 控件将被替换为 Windows 窗体 Label 控件,并且将 BorderStyle 属性设置为 FixedSingle,
可以采用与原始 Shape 控件不同的顺序将 Label 控件添加到窗体。对于重叠形状,可能需要调用
Shape 属性设置为“2-Oval”或“3-Circle”的 Shape 控件不能升级,并由作为占位符的 Label 控件进行替换。可以用 .NET Framework 内置的图形函数替换 Shape 控件。有关更多信息,请参见图形(针对 Visual Basic 6.0 用户)。