Recipe 9.28. Substitutions for Obsolete Visual Basic 6.0 Features


Problem

You used to use a lot of form-based drawing features in Visual Basic 6.0, but many of them seem to be missing from the .NET versions of Visual Basic.

Solution

Sample code folder: Chapter 09\VB6Replacements

GDI+ is a full-featured drawing package that provides easier access to form-based drawing than Visual Basic 6.0 did. Unfortunately, finding the replacements for some of VB 6's form-based drawing features takes a bit of work. This recipe discusses some of the more significant replacements.

Discussion

Most of the replacement features involve GDI+ drawing, although you can simulate some older features using Label controls. The features discussed in this section focus on those methods and controls that were used directly on a form. In .NET, any of the drawing commands that you use on the form's surface can also be used on any control.

Any discussion that mentions "drawing on the form" refers to drawing through the form's Graphics object. Such drawing is usually done in the form's Paint event handler, which provides you with a Graphics object:

 Private Sub Form1_Paint(ByVal sender As Object, _       ByVal e As System.Windows.Forms.PaintEventArgs) _       Handles Me.Paint    ' ----- Draw a   line.    e.Graphics.DrawLine(…) End Sub  

You can also create a Graphics object at any time in other event handlers and methods using the form's CreateGraphics() method:

 Dim formCanvas As Graphics = Me.CreateGraphics() e.Graphics.DrawLine(…) ' ----- Properly dispose of the graphics canvas. formCanvas.Dispose() 

Let's look at some of the specific replacements:


Line controls

There are two replacements for Visual Basic 6.0 Line controls. If your line is horizontal or vertical, you can use a Label control with the BackColor property set to the line color you need. Adjust the width or height of the label as needed to increase the thickness of the line. Be sure to clear the Text property and set the AutoSize property to False.

If you need diagonal lines, you can draw them on the form surface in the form's Paint event using the DrawLine() method.


Shape controls

There is no direct control replacement for the Visual Basic 6.0 Shape controls. Rectangular or elliptical shapes can be drawn directly on the form using the DrawRectangle() and DrawEllipse() methods. The related FillRectangle() and FillEllipse() methods draw filled shapes, with no edge lines.

There is no drawing command that can generate a rectangle with rounded corners. You must create it yourself using DrawLine() and DrawArc() method calls. You can also build this shape as a GraphicsPath object. Here is a method that draws a rounded rectangle directly on a graphics surface. The rounded corner has a radius of five pixels (units, actually):

 Private Sub DrawRoundedRectangle( _       ByVal sourceRectangle As Rectangle, _       ByVal canvas As Graphics, ByVal usePen As Pen)    ' ----- Draw a rounded rectangle.    Dim saveState As Drawing2D.GraphicsState    ' ----- Move the origin to the upper-left corner    '       of the rectangle.    saveState = canvas.Save()    canvas.TranslateTransform(sourceRectangle.Left, _       sourceRectangle.Top)    With sourceRectangle       ' ----- Draw the four edges, starting from the top       '       and moving clockwise.       canvas.DrawLine(usePen, 5, 0, .Width - 5, 0)       canvas.DrawLine(usePen, .Width, 5, .Width, .Height - 5)       canvas.DrawLine(usePen, .Width - 5, .Height, 5, .Height)       canvas.DrawLine(usePen, 0, .Height - 5, 0, 5)       ' ----- Draw the four corners, starting from the       '       upper left and moving clockwise.       canvas.DrawArc(usePen, 0, 0, 10, 10, 180, 90)       canvas.DrawArc(usePen, .Width - 10, 0, 10, 10, 270, 90)       canvas.DrawArc(usePen, .Width - 10, .Height - 10, _          10, 10, 0, 90)       canvas.DrawArc(usePen, 0, .Height - 10, 10, 10, 90, 90)    End With    ' ----- Restore the original   graphics canvas.    canvas.Restore(saveState) End Sub 

This code draws a 100-by-100-unit rounded rectangle at position (10,10) on the form's surface:

 Private Sub Form1_Paint(ByVal sender As Object, _       ByVal e As System.Windows.Forms.PaintEventArgs) _       Handles Me.Paint    DrawRoundedRectangle(New Rectangle(10, 10, 100, 100), _       e.Graphics, Pens.Black) End Sub 

Figure 9-39 shows the output from this code.

Figure 9-39. A manually rounded rectangle



Cls() method

To clear the entire graphics surface, use the Clear() method. You pass it the color used to clear the surface:

      e.Graphics.Clear(Color.White) 


Scale() method

To change the coordinate system on the form's surface, use the Graphics object's ScaleTransform() method. You can also supply a custom matrix transformation by assigning the Graphics object's transform property.


PSet() method

There is no method that can draw a single pixel on a graphics surface. You can simulate it using the DrawLine(), DrawRectangle(), or FillRectangle() methods and providing very precise coordinates. Another way to draw a single point is to create a single-point bitmap and draw the bitmap onto the canvas. The Bitmap class does have a SetPixel method:

 ' ----- Draw a red pixel at (5,5). Dim tinyBitmap As New Bitmap(1, 1) tinyBitmap.SetPixel(0, 0, Color.Red) e.Graphics.DrawImageUnscaled(tinyBitmap, 5, 5) tinyBitmap.Dispose() 


Point() method

While the Graphics object does not let you query the color of an individual pixel, you can do so with a Bitmap object. This object's GetPixel() method returns a Color object for the specified pixel.


Line() method

Replaced by the DrawLine() method.


Circle() method

Replaced by the DrawEllipse() and FillEllipse() methods.


PaintPicture() method

Replaced by the DrawImage() method.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net