|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Drawing Freehand With The MouseThe Testing Department is on the phone. Your new program, SuperDuperGraphicsPro, is fine, but how about letting the user draw freehand with the mouse? Hmm, you think, how does that work? As the user moves the mouse, you can use the Line statement to connect the mouse locations passed to your program in the MouseMove event handler. Note that you are not passed every pixel the mouse travels over, so you must connect the dots, so to speak, rather than setting individual pixels as a lot of programmers think. Heres an example where we draw freehand with the mouse. Because we should only draw after the mouse button has gone down, we set up a Boolean flag, blnDrawFlag, in the (General) part of the form: Dim blnDrawFlag As Boolean We set that flag to False when the form first loads:
Private Sub Form_Load()
blnDrawFlag = False
End Sub
When the user presses the mouse button, we set the current drawing location (CurrentX, CurrentY) to the location of the mouse (so we dont start drawing from the origin of the form by mistake), and set blnDrawFlag to True in the MouseDown event handler:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
CurrentX = X
CurrentY = Y
blnDrawFlag = True
End Sub
When the user moves the mouse, we check if the blnDrawFlag is True in the MouseMove event, and if so, draw a line from the current drawing location to the current (X, Y) position (if you omit the first coordinate of a line, Visual Basic uses the current drawing location):
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If blnDrawFlag Then Line -(X, Y)
End Sub
When the mouse button goes up, we set blnDrawFlag to False in the MouseUp event:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
blnDrawFlag = False
End Sub
Running this program results in the kind of display you see in Figure 18.9, where were letting the user draw with the mouse. Note that weve also changed the mouse cursor into a cross in this drawing example, by setting the forms MousePointer property to 2.
Now were drawing freehand in Visual Basic. The code for this example is located in the drawfreehand folder on this books accompanying CD-ROM. Filling Figures With ColorTo fill figures with color, you can use the FillColor property of forms and picture boxes, along with the FillStyle property to set the type of fill you want. Lets see an example. Here, well draw a circle and a box in a form in the default drawing color (black) and fill those figures with solid blue when the user clicks a button, Command1. First, we set the forms FillColor property to blue:
Private Sub Command1_Click()
FillColor = RGB(0, 0, 255)
Then we specify we want figures colored in solidly by setting the FillStyle property to vbSolid (for more on FillStyle, see the next topic in this chapter):
Private Sub Command1_Click()
FillColor = RGB(0, 0, 255)
FillStyle = vbFSSolid
Finally we draw the box and the circle:
Private Sub Command1_Click()
FillColor = RGB(0, 0, 255)
FillStyle = vbFSSolid
Line (0, 0)-(ScaleWidth / 2, ScaleHeight / 2), , B
Circle (3 * ScaleWidth / 4, 3 * ScaleHeight / 4), ScaleHeight / 4
End Sub
Thats itnow the preceding code will draw a box and a circle with a black border, filled in blue, as shown in Figure 18.10.
Filling Figures With PatternsYou can use the form and picture box FillStyle property to set the fill pattern in Visual Basic graphics. Here are the possibilities:
Figure 18.11 shows what the fill patterns look like. The default, VbFSTransparent, means that by default figures are not filled in.
Setting Figure Drawing Style And Drawing WidthThe Aesthetic Design Department is on the phone. Cant you do something about the graphics figures in your program? Maybe make themdotted? You think, dotted? Visual Basic can help: just set the DrawStyle property in forms or picture boxes. Here are the possible values for that property:
You can also set the drawing width with the DrawWidth property. Heres an example where we set the DrawStyle property to dashed and draw two figures in a form, a box and a circle:
Private Sub Command1_Click()
DrawStyle = vbDash
Line (0, 0)-(ScaleWidth / 2, ScaleHeight / 2), , B
Circle (3 * ScaleWidth / 4, 3 * ScaleHeight / 4), ScaleHeight / 4
End Sub
The result of the preceding code appears in Figure 18.12.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |