Painter

Create one label, put it in the lower right-hand corner of the form, and change its (Name) property to "lblMode". Make the caption initially blank (it actually doesn't matter, as the caption will be overwritten as soon as the form loads).

In (General) (Declarations):

Dim DrawingMode As Integer
Dim a1 As Integer
Dim b1 As Integer
Dim a2 As Integer
Dim b2 As Integer
Dim r As Integer

Create a new procedure called DisplayDrawingMode (with the Code Windows showing, click the Tools drop-down menu, select Add Procedure, type "DisplayDrawingMode" and hit ENTER, leaving all the settings as they are) and put the following code into it:

If DrawingMode = 1 Then
  lblMode.Caption = "Freeform"
ElseIf DrawingMode = 2 Then
  lblMode.Caption = "Line"
ElseIf DrawingMode = 3 Then
  lblMode.Caption = "Box"
Else ' DrawingMode = 4
  lblMode.Caption = "Circle"
End If

In the Form's Load procedure (Form_Load):

DrawingMode = 1
DisplayDrawingMode

In the Form's MouseDown procedure:

a1 = X
b1 = Y
r = 0

In the Form's MouseUp procedure:

If Button = 2 Then ' Clicked right mouse button
  DrawingMode = DrawingMode + 1 ' Next drawing mode
  If DrawingMode > 4 Then DrawingMode = 1
  DisplayDrawingMode
End If

In the Form's MouseMove procedure:

If Button = 1 Then
  If DrawingMode = 1 Then ' Freeform
    Line (a1, b1)-(X, Y)
    a1 = X
    b1 = Y
  ElseIf DrawingMode = 2 Then ' Line
    Line (a1, b1)-(a2, b2), Form1.BackColor ' Erase old line
    a2 = X ' Update values of a2 and b2
    b2 = Y
    Line (a1, b1)-(a2, b2) ' Draw new line
  ElseIf DrawingMode = 3 Then ' Box
    Line (a1, b1)-(a2, b2), Form1.BackColor, B
    a2 = X
    b2 = Y
    Line (a1, b1)-(a2, b2), , B
  Else ' DrawingMode = 4   Circle
    Circle (a1, b1), r, Form1.BackColor
    If Abs(X - a1) > Abs(Y - b1) Then ' If moved farther horizontally
      r = Abs(X - a1) ' Radius = horizontal movement
    Else ' Otherwise
      r = Abs(Y - b1) ' Radius = vertical movement
    End If
    Circle (a1, b1), r
  End If
End If

Note the following table of possible values for Button:

ValueMeaning
0No buttons are pressed
1The left button caused the event
2The right button caused the event
3The left and right buttons caused the event
4The middle button caused the event
5The left and middle buttons caused the event
6The right and middle buttons caused the event
7All three buttons caused the event

Extra Improvements:

  1. Include a "Filled Box" selection in the lblMode list of drawing modes. It should be very similar to the code for a regular box.
  2. Add a menu to the program, with a selection to change the color. Add a CommonDialog control into your project (right-click in the Toolbox, select Components, and put a check-mark next to the "Microsoft Common Dialog Control"), and use its ShowColor procedure (e.g. CommonDialog1.ShowColor), then get its Color property and use that as the color to use when drawing.

Visual Basic by Example is hosted by GeoCities
1