Drawing Simple Lines and Shapes (Visual Basic)

Take the following steps to add code that lets you draw a line, rectangle and ellipse on the bitmap.

1.

Start with the project that you created in Loading and Displaying an Image.

2.

Add the LEAD Raster 3D DrawEffect Object Library (14.5) to your project.

 

On the Project pull-down menu, use the References option, and select the LEAD Raster 3D DrawEffect Object Library (14.5).

3.

Add the following code to the general declarations section of your project.

Public RasterFxd As LEADRasterFXD

4.

Add the following code to the end of the Form_Load event handler.

'Create the RasterFxd Object
Set RasterFxd = CreateObject("LEADRasterFXD.LEADRasterFXD.")

5.

Add the following form-level variables to the declarations procedure of the general object in your main form:

Dim DrawObject As Integer 'The object we are drawing
Dim StartX As Single 'Starting X position
Dim StartY As Single 'Starting Y position
Dim EndX As Single 'Ending X position
Dim EndY As Single 'Ending Y position

6.

Code the LEADRasterView control's MouseDown event as follows. This code selects a different drawing object each time the event occurs.

'Save the starting position.
StartX = x
StartY = y
EndX = x
EndY = y
'Cycle through the types of drawing objects.
Select Case DrawObject
Case 0
  DrawObject = 1 'Line
Case 1
  DrawObject = 2 'Rectangle
Case 2
  DrawObject = 0 'Ellipse
Case Else
  DrawObject = 0
End Select
RasterFxd.DstLeft = 0
RasterFxd.DstTop = 0
RasterFxd.DstRight = LEADRasterView1.DstWidth
RasterFxd.DstBottom = LEADRasterView1.DstHeight
RasterFxd.SrcLeft = 0
RasterFxd.SrcTop = 0
RasterFxd.SrcRight = LEADRasterView1.Raster.BitmapWidth
RasterFxd.SrcBottom = LEADRasterView1.Raster.BitmapHeight
RasterFxd.ScaleMode = 3

7.

Code the LEADRasterView control's MouseMove event as follows. This code uses DRAWMODE_INVERT for the DrawMode, which means that pixel colors are inverted. Thus, the drawing methods can erase the previous object and draw a new one.

'Declare local variables.
Dim OldEndX, OldEndY
Dim OldDrawX, OldDrawY, OldWidth, OldHeight
Dim DrawX, DrawY, NewWidth, NewHeight
Dim hDC As Long
If Button = 1 Then
  'Set the drawing styles.
  RasterFxd.DrawPenStyle = DRAWPENSTYLE_SOLID
  RasterFxd.DrawMode = DRAWMODE_INVERT
  RasterFxd.DrawFillStyle = DRAWFILLSTYLE_TRANSPARENT
  RasterFxd.DrawPersistence = False 'On the window, not the bitmap
  'Save the previous ending mouse position.
  OldEndX = EndX
  OldEndY = EndY
  'Get the current mouse position.
  EndX = x
  EndY = y
  'Calculate the origin of the current object.
  If EndX > StartX Then
      DrawX = StartX
  Else
      DrawX = EndX
  End If
  If EndY > StartY Then
      DrawY = StartY
  Else
      DrawY = EndY
  End If
  'Calculate the origin of the previous object.
  If OldEndX > StartX Then
      OldDrawX = StartX
  Else
      OldDrawX = OldEndX
  End If
  If OldEndY > StartY Then
      OldDrawY = StartY
  Else
      OldDrawY = OldEndY
  End If
  'Calculate the height and width of the current object.
  NewHeight = Abs(StartY - EndY)
  NewWidth = Abs(StartX - EndX)
  'Calculate the height and width of the previous object.
  OldHeight = Abs(StartY - OldEndY)
  OldWidth = Abs(StartX - OldEndX)
  'Erase the old object and draw the new one.
  hDC = LEADRasterView1.GetClientDC()
  Select Case DrawObject
  Case 0 'Ellipse
    RasterFxd.DrawEllipse Nothing, hDC, OldDrawX, OldDrawY, OldWidth, OldHeight
    RasterFxd.DrawEllipse Nothing, hDC, DrawX, DrawY, NewWidth, NewHeight
  Case 1 'Line
    RasterFxd.DrawLine Nothing, hDC, StartX, StartY, OldEndX, OldEndY
    RasterFxd.DrawLine Nothing, hDC, StartX, StartY, EndX, EndY
  Case 2 'Rectangle
    RasterFxd.DrawRectangle Nothing, hDC, OldDrawX, OldDrawY, OldWidth, OldHeight
    RasterFxd.DrawRectangle Nothing, hDC, DrawX, DrawY, NewWidth, NewHeight
  Case Else
  End Select
  LEADRasterView1.ReleaseClientDC
End If

8.

Code the LEADRasterView control's MouseUp event as follows. This code sets the drawing style and draws the object on the bitmap.

'Declare local variables.
Dim DrawX, DrawY, NewWidth, NewHeight
'Set the drawing style.
RasterFxd.DrawPenStyle = DRAWPENSTYLE_SOLID
RasterFxd.DrawPenWidth = 2
RasterFxd.DrawPenColor = RGB(255, 0, 0) 'Red
RasterFxd.DrawMode = DRAWMODE_COPY_PEN
RasterFxd.DrawFillColor = RGB(0, 255, 0) 'Green
RasterFxd.DrawFillStyle = DRAWFILLSTYLE_HORIZONTAL_LINE
RasterFxd.DrawPersistence = True 'On the bitmap
'Get the current mouse position
EndX = x
EndY = y
'Determine the origin of the object.
If EndX > StartX Then
  DrawX = StartX
Else
  DrawX = EndX
End If
If EndY > StartY Then
  DrawY = StartY
Else
  DrawY = EndY
End If
'Determine the height and width of the object.
NewHeight = Abs(StartY - EndY)
NewWidth = Abs(StartX - EndX)
'Draw the object
Select Case DrawObject
Case 0 'Ellipse
  RasterFxd.DrawEllipse LEADRasterView1.Raster, 0, DrawX, DrawY, NewWidth, NewHeight
Case 1 'Line
  RasterFxd.DrawLine LEADRasterView1.Raster, 0, StartX, StartY, EndX, EndY
Case 2 'Rectangle
  RasterFxd.DrawRectangle LEADRasterView1.Raster, 0, DrawX, DrawY, NewWidth, NewHeight
Case Else
End Select
LEADRasterView1.ForceRepaint

6.

Run your program to test it.