Drawing Pad

 

We can create a simple virtual drawing program using Visual Basic 6 as shown in the diagram below. We called it drawing pad.

In this program, the user needs to fill in all the coordinates and chooses a color before he or she  can proceed to draw the required shape. If he or she forgets to fill in the coordinates or chooses a color, he or she will be prompted to do so.

To create the drawing pad, you need to insert a common dialog control, a picture box, four text boxes , six command buttons and the necessary labels. The function of the common dialog control is to assist the users to choose colors. The text boxes are for the user to enter the coordinates and the picture box is to display the pictures drawn.

The syntax to draw a straight line is Line, and the syntax is as follows:

Picture1.Line (x1, y1)-(x2, y2), color

where  picture1 is the picture box, (x1,y1) is the coordinates of the starting point, (x2, y2) is the ending point and color understandably is the color of the line.

The syntax to draw a non-solid rectangle is

Picture1.Line (x1, y1)-(x2, y2), color, B

The syntax to draw a solid rectangle is

Picture1.Line (x1, y1)-(x2, y2), color, BF

The syntax to draw a circle is

Picture1.Circle (x3, y3), r, color

Where (x3, y3) is the center of the circle and r is the radius.

if you wish to draw a solid circle and fill it with the selected color, then add two more lines to the above syntax:

Picture1.FillStyle = vbSolid
Picture1.FillColor = color

The syntax to clear the picture is

Picture1.Cls

The Interface

The code

 

Private Sub cmd_Rectangle_Click()
x1 = Text1.Text
y1 = Text2.Text
x2 = Text3.Text
y2 = Text4.Text
Picture1.Line (x1, y1)-(x2, y2), color, B
End Sub

Private Sub cmd_Color_Click()
CommonDialog1.Flags = &H1&
CommonDialog1.ShowColor
color = CommonDialog1.color

End Sub

Private Sub cmd_Circle_Click()
On Error GoTo addvalues
x3 = Text5.Text
y3 = Text6.Text
r = Text7.Text

Picture1.FillStyle = vbSolid
Picture1.FillColor = color
Picture1.Circle (x3, y3), r, color
Exit Sub

addvalues:
MsgBox ("Please fill in the coordinates ,the radius and the color")

End Sub

Private Sub Command5_Click()
Picture1.Cls
End Sub

Private Sub cmd_SolidRect_Click()
x1 = Text1.Text
y1 = Text2.Text
x2 = Text3.Text
y2 = Text4.Text
Picture1.Line (x1, y1)-(x2, y2), color, BF
End Sub

 

 

[Back to Sample VB Programs]

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net

[Privacy Policy]