Section 13 Using Methods

A Method is a way of doing something to an object. You should use the Help Contents to find out more about Methods and when to use them.

Problem 13

A program is required that will let you draw a picture onto the form. This is the beginning of a "paint program".

Planning

This program requires a command button object and a picture object. As well, to draw the lines, a Method is needed. The method used in this case is the Line method. At this stage, you should check out the help for Line Method.

The next thing to be considered is the Event that is to be used. Up until now, we have used the Click event.

This time we are going to use the Mouse_Down event for the Picture object and the Mouse_Click event for the command button.

Pseudocode

Draw a line to the current mouse position. 

Code

Private Sub PicDraw_MouseDown(Button As Integer, 
   Shift As Integer, X As Single, Y As Single) 
   PicDraw.Line -(X, Y) ' draw a line to (x,y)
End Sub

At this point it is also important to note that the parameters passed into the event include X and Y. These indicate the current mouse position. Thus, when the mouse button is pressed while in the Picture control, a line is drawn from the last position to the current mouse position. (The starting position is 0,0 and is the top left-hand corner.)

Another method you will need to use is the Cls method. This allows you to clear the current picure.

Private Sub CmdClear_Click()
  PicDraw.cls
End Sub

Problems 13

1 Add control buttons or buttons made from image contols that will allow the user to change the colour of the line drawn.

You will need to use the QBColor(n) function. This can be used in the ForeColor property of the picture object. (Use Help Line to get details).

Syntax

QBColor(color)

The color argument has these settings:

Number

Color

Number

Color

0 Black 8 Gray
1 Blue 9 Light Blue
2 Green 10 Light Green
3 Cyan 11 Light Cyan
4 Red 12 Light Red
5 Magenta 13 Light Magenta
6 Yellow 14 Light Yellow
7 White 15 Bright White

Example

'To make the line green
PicDraw.forecolor = QBColor(2)

2. Write a program that will use the circle method to allow you to draw circles instead of lines.

Modify this to allow filled in colours with different patterns.

Example

PicDraw.FillColor = QBColor(2)
PicDraw.FillStyle = 0

will let you draw a green filled circle in the PicDraw object.

You will need to use HELP to find out the different patterns available.

 

3. Use the PaintPicture method to paste images into your drawing.

Hint: Read the HELP on PaintPicture.

You must have a picture already in another picture object before you can copy it into your drawing. You can use the picture property of the object to get the picture you need. Then use

PicDraw.PaintPicture PicCopy.Picture, 10, 10

to put a picture in the top corner of your drawing. PicCopy holds the picture you want. PicDraw is where you are drawing.


Author Mike Leishman

Last Modified 28 Junew 1998