Udostępnij za pośrednictwem


Shape Control for Visual Basic 6.0 Users

The Shape control in Visual Basic 6.0 has no equivalent in Visual Basic 2008. However, you can use Graphics methods to achieve the same results, or you can use the Visual Basic Power Packs OvalShape or RectangleShape controls. These controls are available as add-ins.

Conceptual Differences

In Visual Basic 6.0, the Shape control provides an easy way to draw rectangles, circles, and other shapes on a form at design time. The Shape control is a lightweight control, which means that it does not have a Windows handle, also known as an HWnd.

In Visual Basic 2008, there is no equivalent for the Shape control, and lightweight controls are no longer supported. However, there are ways to draw shapes on a form both at design time and at run time.

Note

The Visual Basic Power Packs controls include LineShape, OvalShape, and RectangleShape controls that can be used to replace the Line and Shape controls. In addition to duplicating the behavior of the Visual Basic 6.0 Line and Shape controls, these controls add new capabilities, including gradient fills, run time selection, and run time events.

You can download the Visual Basic Power Packs from the Microsoft Visual Basic 2005 Power Packs page on the MSDN Web site.

At design time, you can draw a square or rectangular shape on a form by adding a Label control and setting the Text property to an empty string, the BorderStyle property to FixedSingle, and the BackColor, Width, and Height to the desired color and dimensions.

At run time, you can draw rectangles, ellipses, and complex shapes in a form's Paint event handler by creating a new object from the Graphics class and calling its methods.

In Visual Basic 6.0, you can use a Shape control to draw a shape on top of a container control such as a PictureBox or Frame control by adding a Shape control to the container.

In Visual Basic 2008, you can achieve the same effect by calling the Graphics methods in the Paint event of the container control.

Code Changes for the Shape Control

The following code examples illustrate the differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.

Code Changes for Drawing Rectangular Shapes

The following code demonstrates drawing a filled rectangle on a form at run time. In the Visual Basic 6.0 example, the Shape control is used; assume that a Line control was added at design time. The Visual Basic 2008 example demonstrates two different methods: using a Label control and using Graphics methods.

Note

In Visual Basic 6.0, the default unit of measurement was twips; in Visual Basic 2008 it is pixels.

' 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.PrivateSub Form2_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) HandlesMyBase.Load
    Dim Shape1 AsNew 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)
EndSub
' Visual Basic' Using Graphics methods.PrivateSub Form2_Paint(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
    ' Draw a solid blue rectangle below the red rectangle.
    e.Graphics.FillRectangle(Brushes.Blue, 14, 120, 200, 100)
EndSub

Code Changes for Drawing a Circle

The following code demonstrates drawing a circle on a form at run time. In the Visual Basic 6.0 example, the Shape control is used; assume that a Shape control was added at design time. The Visual Basic 2008 example uses Graphics methods.

Note

In Visual Basic 6.0 the default unit of measurement was twips; in Visual Basic 2008 it is pixels.

' 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 BasicPrivateSub Form3_Paint(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
        ' Draw a 70 pixel diameter red circle.
        e.Graphics.DrawEllipse(Pens.Red, 0, 0, 70, 70)
    EndSub

Upgrade Notes

When a Visual Basic 6.0 application is upgraded, code that references the Shape control is upgraded to use the OvalShape or RectangleShape controls that are included in the Visual Basic Power Packs library. If a reference to the Shape control is found during upgrade, a warning is issued and you will have to install the library and reference it from your upgraded project.

You can download the Visual Basic Power Packs from the Microsoft Visual Basic 2005 Power Packs page on the MSDN Web site.

See Also

Concepts

Graphics for Visual Basic 6.0 Users