Graphics

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 4.   Post-migration Changes


Graphics is one of the areas that undergo a drastic change when moving from Visual Basic 6.0 to Visual Basic .NET. If applications use graphics functionality in Visual Basic 6.0 and are upgraded to Visual Basic .NET, many of the properties, methods , and events will give a compilation error in Visual Basic .NET. This is because the upgrade wizard does not convert graphics functionality automatically, and these properties are not supported in Visual Basic .NET. The developer has to modify the code in Visual Basic .NET to get it working and should learn the graphics functionality offered by the Microsoft .NET Framework before upgrading graphics applications.

Basics of Graphics

The picturebox and the form control have various properties, methods, and events related to graphics and are based on the Windows Graphics Device Interface (GDI). In Visual Basic .NET (and the Microsoft .NET in general), all the graphics functionality has been removed from the language, and the functionality has been moved to the .NET Framework class library.

The following namespaces are used for graphics functionality in Visual Basic .NET:

  • System.Drawing

  • System.Drawing.Design

  • System.Drawing.Drawing2D

  • System.Drawing.Imaging

  • System.Drawing.Printing

Most of the graphics functionality in Visual Basic .NET follows the same path. A reference to the Graphics object for the control is obtained. This Graphics object contains all the graphics properties, methods for drawing and painting, and so on. The Graphics object resides in the System.Drawing namespace.

The following section gives some examples of graphics functionality in Visual Basic 6.0 and their counterparts in Visual Basic .NET. Note that these are just one of the ways of implementing the solutions in Visual Basic .NET. Note also that the entire code generated by the upgrade wizard is not shown. Only those methods that have to be changed by the developer have been included. When all the following Visual Basic 6.0 examples are upgraded using the upgrade wizard, they give compilation errors in Visual Basic .NET because many of the graphics methods and properties are not supported.

CODE EXAMPLES
Example 1. PictureBox (PaintPicture)

The following Visual Basic 6.0 code illustrates the PaintPicture method of the PictureBox control. The equivalent Visual Basic .NET code shows how the same functionality can be achieved in Visual Basic .NET. The following code is in the PaintPicturePaintBox-VB folder for this chapter:

 graphics/icon01.gif Dim objpic As Picture  Private Sub Form_Load()     Set objpic = LoadPicture("C:\WINNT\Gone Fishing.bmp")     Picture1.AutoRedraw = True     Picture1.PaintPicture objpic, 0, 0  End Sub 

The following Visual Basic .NET code is in the PaintPicturePaintBox-VB.NET-Modified folder for this chapter:

 graphics/icon01.gif Dim objpic As System.Drawing.Image  Private Sub Form1_Load(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles MyBase.Load  objpic = System.Drawing.Image.FromFile_   ("C:\WINNT\Gone Fishing.bmp")  End Sub  Private Sub Picture1_Paint(ByVal sender As Object, _   ByVal e As System.Windows.Forms.PaintEventArgs) _    Handles Picture1.Paint     e.Graphics.DrawImage(objpic, 0, 0)  End Sub 
Example 2. PictureBox (FillColor and FillStyle)

The following Visual Basic 6.0 code illustrates the FillColor and the FillStyle properties of the PictureBox control. The equivalent Visual Basic .NET code shows how the same functionality can be achieved in Visual Basic .NET. The following code is in the FillColorPictureBox-VB folder for this chapter:

 graphics/icon01.gif Private Sub Form_Click()     Picture1.FillColor = vbRed     Picture1.FillStyle = vbFSSolid     Picture1.Circle (Picture1.Width/2, Picture1.Height/_      2), Picture1.Height / 2  End Sub 

The following Visual Basic .NET code is in the FillColorPictureBox-VB.NET-Modified folder for this chapter:

 Private Sub Form1_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles MyBase.Click     Dim objGraphics As Graphics     objGraphics = Picture1.CreateGraphics()     objGraphics.Clear(Picture1.BackColor)     Dim objPen As Pen     objPen = New Pen(Color.Black)     Dim objBrush As New SolidBrush(Color.Red)     Dim x, y, Radius As Integer     objGraphics.DrawEllipse(objPen, Picture1. _      ClientRectangle)     objGraphics.FillEllipse(objBrush, Picture1._      ClientRectangle)  End Sub 

As shown, the FillColor property of the PictureBox control in Visual Basic 6.0 is replaced by the SolidBrush.Color property of the .NET Framework class library. Also, the FillStyle property is replaced by the Pen.Brush property.

Example 3. PictureBox (DrawWidth)

The following Visual Basic 6.0 code illustrates the DrawWidth property of the PictureBox control. The equivalent Visual Basic .NET code shows how the same functionality can be achieved in Visual Basic .NET. The following code is in the DrawWidthPictureBox-VB folder for this chapter:

 graphics/icon01.gif Private Sub Form_Click()     Picture1.DrawWidth = 1     Picture1.Line (Picture1.Width/4, 0)-(Picture1.Width/_      4, Picture1.Height)     Picture1.DrawWidth = 2     Picture1.Line (Picture1.Width/3, 0)-(Picture1.Width/_      3, Picture1.Height)     Picture1.DrawWidth = 4     Picture1.Line (Picture1.Width/2, 0)-(Picture1.Width/_      2, Picture1.Height)  End Sub 

The following Visual Basic .NET code is in the DrawWidthPictureBox-VB.NET-Modified folder for this chapter:

 graphics/icon01.gif Private Sub Form1_Click(ByVal eventSender As_   System.Object, ByVal eventArgs As System.EventArgs)_    Handles MyBase.Click     Dim objGraphics As Graphics     objGraphics = Picture1.CreateGraphics()     Dim objPen As New Pen(Color.Black)     Dim x As Integer     x = Picture1.Size.Width / 4     objPen.Width = 1     objGraphics.DrawLine(objPen, x, 0, x, _      Picture1.Height)     x = Picture1.Size.Width / 3     objPen.Width = 2     objGraphics.DrawLine(objPen, x, 0, x, _      Picture1.Height)     x = Picture1.Size.Width / 2     objPen.Width = 4     objGraphics.DrawLine(objPen, x, 0, x, _      Picture1.Height)  End Sub 

As shown, the DrawWidth property of PictureBox control in Visual Basic 6.0 is replaced by the Pen.Width property of the .NET Framework class library.

Example 4. PictureBox (AutoRedraw)

The following Visual Basic 6.0 code illustrates the AutoRedraw property of the PictureBox control. This property has been set to true during design time and hence is not visible in the code. The equivalent Visual Basic .NET code shows how the same functionality can be achieved. The following code is in the AutoRedrawPictureBox-VB folder for this chapter:

 graphics/icon01.gif Private Sub Form_Click()     Picture1.FillColor = vbRed     Picture1.FillStyle = 0     Picture1.Circle (Picture1.Width/2, Picture1.Height/_      2), Picture1.Height / 2  End Sub 

The following Visual Basic .NET code is in the AutoRedrawPicture-Box-VB.NET-Modified folder for this chapter:

 graphics/icon01.gif Dim objBackGroundBitmap As Bitmap  Private Sub Form1_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles MyBase.Click     Dim objGraphics As Graphics = Graphics.FromImage_      (objBackGroundBitmap)     objGraphics.DrawEllipse(Pens.Black, Picture1. _      ClientRectangle)     objGraphics.FillEllipse(Brushes.Red, Picture1._      ClientRectangle)     Picture1.Refresh()  End Sub  Protected Overrides Sub OnPaint(ByVal e As System._   Windows.Forms.PaintEventArgs)     MyBase.OnPaint(e)  End Sub  Private Sub Picture1_Paint(ByVal sender As Object, _   ByVal e As System.Windows.Forms.PaintEventArgs)_    Handles Picture1.Paint     e.Graphics.DrawImage(objBackGroundBitmap, 0, 0)  End Sub  Private Sub Form1_Load(ByVal sender As Object, ByVal e_   As System.EventArgs) Handles MyBase.Load     If objBackGroundBitmap Is Nothing Then        objBackGroundBitmap = New Bitmap_         (Picture1.Size.Width, Picture1.Size.Height)     End If  End Sub 

A bitmap object is used to store the image of the PictureBox control to achieve the same functionality.

Example 5. Command Button (DisabledPicture Property)

The following Visual Basic 6.0 code illustrates the DisabledPicture property of the Command button control. This property has been set to a particular bitmap image during design time and hence is not visible in the code. The equivalent Visual Basic .NET code shows how the same functionality can be achieved in Visual Basic .NET. The following code is in the DisablePictureCommandButton.VB folder for this chapter:

 graphics/icon01.gif Private Sub Command1_Click()     End  End Sub  Private Sub Command2_Click()     Command1.Enabled = False  End Sub  Private Sub Command3_Click()     Command1.Enabled = True  End Sub 

The following Visual Basic .NET code is in the DisablePictureCommandButton.VB.NET-Modified folder for this chapter:

 graphics/icon01.gif Dim backgroundbitmap As New Bitmap_   ("C:\WINNT\Gone Fishing.bmp")  Private Sub Command1_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles Command1.Click     End  End Sub  Private Sub Command2_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles Command2.Click     Command1.Enabled = False     Dim g As Graphics = Command1.CreateGraphics()     Command1.Refresh()  End Sub  Private Sub Command3_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles Command3.Click     Command1.Enabled = True  End Sub  Private Sub Command1_Paint(ByVal sender As Object, _   ByVal e As System.Windows.Forms.PaintEventArgs) _    Handles Command1.Paint     If Command1.Enabled = False Then        e.Graphics.DrawImage(backgroundbitmap, 0, 0)        Dim A As PointF        A = New PointF((Command1.Size.Width) / 3, _         (Command1.Size.Height))        e.Graphics.DrawString("Exit", Me.Font, New _         SolidBrush(Color.DarkGray), A)     End If  End Sub 

In the preceding code, when the appropriate command buttons are clicked, the command button1 is enabled and disabled. Also the Refresh method of the command1 button is invoked. All the logic for drawing and clearing the image by the command button is present in the Command1_Paint event, which always gets executed when the refresh method is called.

In the code in the Command1_Paint event, first, a Graphics object is obtained from the paint event arguments passed to the event. The DrawImage method of the graphics object is used with the bitmap object being an argument to this method.

Example 6. Command Button (DownPicture Property)

In Visual Basic 6.0, we can specify an image to be displayed on a button when the mouse button is in the down position on the button. This is set at design time, and hence there is no Visual Basic 6.0 code for it. The same can be achieved in Visual Basic .NET as shown in the following code snippet. This code is in the DownPictureCommandButton-VB.NET-Modified folder for this chapter:

 graphics/icon01.gif Dim backgroundbitmap As New Bitmap _    ("C:\WINNT\Gone Fishing.bmp")   Dim bMouseDown = False   Private Sub Button1_Paint(ByVal sender As Object, _    ByVal e As System.Windows.Forms.PaintEventArgs) _     Handles Button1.Paint      If bMouseDown Then         e.Graphics.DrawImage(backgroundbitmap, 0, 0)         Dim A As PointF         A = New PointF((Button1.Size.Width)/4, _          (Button1.Size.Height) / 4)         e.Graphics.DrawString(Button1.Text, Me.Font, _          New SolidBrush(Color.Black), A)  Else          'Button1.Image = Nothing  End If   End Sub   Private Sub Button1_MouseUp(ByVal sender As Object,_    ByVal e As System.Windows.Forms.MouseEventArgs) _     Handles Button1.MouseUp      bMouseDown = False      Button1.Image = Nothing      Button1.Refresh()   End Sub   Private Sub Button1_MouseDown(ByVal sender As Object,_    ByVal e As System.Windows.Forms.MouseEventArgs) _     Handles Button1.MouseDown      bMouseDown = True      Button1.Refresh()   End Sub 

The logic for painting an image over a button is coded in the button_paint event. A Boolean flag is used to indicate that the mouse button is in the down position. The graphics object is obtained from the paint event arguments. The DrawImage method of the graphics object is called with the bitmap object (containing the bitmap file name ) as its argument.

Example 7. FloodFill Method

The following Visual Basic 6.0 code illustrates the FloodFill method of the GDI32 library. We draw a triangle on a form by passing hdc values to the FloodFill method. The equivalent Visual Basic .NET code shows how the same functionality can be achieved in Visual Basic .NET; it is in the FloodFill-VBfolder for this chapter:

 graphics/icon01.gif Private Sub Command1_Click()     End  End Sub  Private Sub Form_Click()     ScaleMode = vbPixels     ForeColor = vbBlack     Line (100, 50)-(300, 50)     Line -(200, 200)     Line -(100, 50)     FillStyle = vbFSSolid     FillColor = RGB(128, 128, 255)     FloodFill hDC, 200, 100, ForeColor  End Sub  Private Sub Picture1_Click()     Picture1.ScaleMode = vbPixels     Picture1.ForeColor = vbBlack     Picture1.Line (100, 50)-(300, 50)     Picture1.Line -(200, 200)     Picture1.Line -(100, 50)     Picture1.FillStyle = vbFSSolid     Picture1.FillColor = RGB(255, 128, 255)     FloodFill Picture1.hDC, 200, 100, ForeColor  End Sub 

The following equivalent Visual Basic .NET code is in the FloodFill-VB.NET-Modified folder for this chapter:

 graphics/icon01.gif Declare Sub FloodFill Lib "GDI32" (ByVal hDC As Long, _   ByVal X As Long, ByVal Y As Long, ByVal crColor As_    Long) 

The above declaration appears in the module Module1.bas .

 graphics/icon01.gif Private Sub Command1_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles Command1.Click     End  End Sub  Private Sub Form1_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles MyBase.Click     Dim g As Graphics = Me.CreateGraphics()     g.DrawLine(Pens.Black, 100, 50, 300, 50)     g.DrawLine(Pens.Black, 300, 50, 200, 200)     g.DrawLine(Pens.Black, 200, 200, 100, 50)     FloodFill(g.GetHdc(), 200, 100, Color.Beige.ToArgb())  End Sub  Private Sub Picture1_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs)_    Handles Picture1.Click     Dim g As Graphics = Picture1.CreateGraphics()     g.DrawLine(Pens.Black, 100, 50, 300, 50)     g.DrawLine(Pens.Black, 300, 50, 200, 200)     g.DrawLine(Pens.Black, 200, 200, 100, 50)     FloodFill(g.GetHdc(), 200, 100, Color.Beige.ToArgb())  End Sub 

The following declaration appears in the module Module1.bas file:

 graphics/icon01.gif Option Strict Off  Option Explicit On  Module Module1  Friend Declare Ansi Function FloodFill Lib "gdi32" (_   ByVal hDC As IntPtr, ByVal X As Integer, ByVal Y As _    Integer, ByVal crColor As Integer) As Integer  End Module 

These code snippets show how functionality similar to the FloodFill can be coded in Visual Basic .NET and how the GDI library functions provided by the Win32 library can be accessed.


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

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