DCS4AVISUAL BASICLesson 13
Graphic Methods
Goals:
- Learn Graphic methods such as Circle and Line
- Use Mouse Events
- Store values in Arrays
- Store values in Binary files
Circle Method
The Circle method is a procedure that can be used to draw circles on an object, such as a form, a Picture Box control, or the Printer object. Syntax: object.Circle (x,y), radius where x and y are screen coordinates and radius is the radius of the circle.
Line Method
The Line method is a procedure that can be used to draw lines on an object, such as a form, a Picture Box control, or the Printer object. There are two versions of the line method which appear below.
Syntax:object.Line (x,y) – (x’,y’)Draws a line between screen coordinates (x,y) and (x’,y’).
Syntax:object.Line – (x,y)Draws a line between the last point drawn and coordinates (x,y).
The coordinates of the last point drawn are saved in the CurrentX and CurrentY properties of the object.
Mouse Events
In VB, the mouse events MouseDown, MouseUp have parameters that tell you which buttons are pressed or released, as well as the coordinates of the point where the press or release occurred. The MouseMove event tells you the current coordinates of the mouse cursor (X As Single and Y As Single)
Flags
A flag is a variable used as a signal in a program. A flag may be True or False. If the flag is set, its’ value is True (or –1 in VB). Flags are used in IF statements. They are usually global variables so that many different event procedures can check their value and take action based on the state of the flag.
The Drawing Program
Description:
Allows the user to draw lines, rectangles, triangles, and circles on a form using the mouse events. The coordinates of the endpoints of the lines drawn are stored in arrays. When the drawing is complete the coordinates are stored in a file. Drawings can be loaded from files.
You will use the LINE, CIRCLE methods as well as FLAGS indicating the current state of the drawing. Mouse events UP, DOWN, MOVE and CLICK will play an important role in the drawing program.
An introduction to the drawing program will be provided. You will then have the opportunity to customize and enhance this drawing program for your purposes.
Basic Features will include:
- single lines from point to point
- continuous lines from point to point to point ...
- freestyle lines, lines drawn to follow the mouse movement as long as the button is depressed
- triangles
- rectangles, sides parallel to the form, drawn from one corner to another
- circles, drawn by clicking the center and a point on the circle
- circles, drawn by clicking the endpoints of a diameter
Also include these additional features:List Your Customized Additional Features:
changing the line width
changing the colour of the line drawn
printing the form
providing a grid on the form for alignment purposes
clearing the form
saving the form
opening the form
Programming steps:
1. Define the user interface (menu items)
2. Declare and initialize the variables
3. Code the routines that control the drawing of circles
4. Code the routines that control the drawing of lines
5. Code the grid, colours, and line widths
6. Code the mouse event routines
7. Code the File event routines: Save & Open
8. Code any additional features
______
Assignment
Copy the existing program i:\dcs4a\line drawing program to your G:\ drive. Examine and make necessary changes to suit your needs and include the additional features mentioned above.
First consider the menu structure. Identify the components that will fit together. i.e. Circles- two types, Lines- 3 types + two shapes. Lines can share the same menu or be split into two.
Use either Dialog boxes or your own File Save/Open procedures.
Code and comments have been provided for several procedures. Some code may need to be modified, other code needs to be written. Comments have been provided as guidance in most procedures.
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
gfDrawing = True 'Flag used by the MouseDown handler
DrawCircle X, Y
'Set the form's CurrentX, CurrentY properties
CurrentX = X
CurrentY = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If gfDrawing Then
Line -(X, Y)
DrawCircle X, Y
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
gfDrawing = False
Debug.Print gfDrawing
End Sub
Private Sub mnuOpen_Click()
'Collect filename from the user
Dim strFn As String
strFn = UCase$(Trim$(InputBox("Filename", "OpenFile")))
'Open Binary file for input
Open strFn For Binary Access Read As #1
'Save actual number of points
Get #1, , gnNumPoints
'Local variable for loop
Dim i As Integer
'Loop to actual number of points
For i = 1 To gnNumPoints
'Collect coordinates from file
Get #1, , gsngX(i)
Get #1, , gsngY(i)
Next i
Close #1
'Reset form caption
frmDrawLine.Caption = gstrTitle & "-" & strFn
frmDrawLine.Cls
DrawLines
End Sub
Private Sub mnuSaveAs_Click()
'Collect Filemane from user
Dim strFn As String
strFn = UCase$(Trim$(InputBox("Filename", "OpenFile")))
'Open binary file for output
Open strFn For Binary Access Write As #1
'Save actual number of points
Put #1, , gnNumPoints
'Local variable for loop
Dim i As Integer
For i = 1 To gnNumPoints
'Save coordinates
Put #1, , gsngX(i)
Put #1, , gsngY(i)
Next i
Close #1
'Reset the form caption
frmDrawLine.Caption = gstrTitle & "-" & strFn
End Sub
16/11/18