599 604




Visual Basic 6 Black Book:Working With Graphics
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Drawing Freehand With The Mouse
The 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.
Here’s 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 don’t 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 we’re letting the user draw with the mouse. Note that we’ve also changed the mouse cursor into a cross in this drawing example, by setting the form’s MousePointer property to 2.

Figure 18.9   Drawing freehand with the mouse.
Now we’re drawing freehand in Visual Basic. The code for this example is located in the drawfreehand folder on this book’s accompanying CD-ROM.

Filling Figures With Color
To 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.
Let’s see an example. Here, we’ll 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 form’s 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


That’s it—now the preceding code will draw a box and a circle with a black border, filled in blue, as shown in Figure 18.10.


Figure 18.10   Filling figures with color.

TIP:  If you use the F argument when drawing boxes with the Line method, Visual Basic will use the color you specify for the box’s drawing color (and if you didn’t specify a color, it will use the current ForeGround color) instead of the FillColor.

Filling Figures With Patterns
You can use the form and picture box FillStyle property to set the fill pattern in Visual Basic graphics. Here are the possibilities:

•  VbFSSolid—0; solid
•  VbFSTransparent—1 (the default); transparent
•  VbHorizontalLine—2; horizontal line
•  VbVerticalLine—3; vertical line
•  VbUpwardDiagonal—4; upward diagonal
•  VbDownwardDiagonal—5; downward diagonal
•  VbCross—6; cross
•  VbDiagonalCross—7; diagonal cross

Figure 18.11 shows what the fill patterns look like. The default, VbFSTransparent, means that by default figures are not filled in.

Figure 18.11   The Visual Basic fill patterns.
Setting Figure Drawing Style And Drawing Width
The Aesthetic Design Department is on the phone. Can’t you do something about the graphics figures in your program? Maybe make them—dotted? 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:

•  vbSolid—1 (the default); solid (the border is centered on the edge of the shape)
•  vbDash—2; dash
•  vbDot—3; dot
•  vbDashDot—4; dash-dot
•  vbDashDotDot—5; dash-dot-dot
•  vbInvisible—5; invisible
•  vbInsideSolid—6; inside solid (the outer edge of the border is the outer edge of the figure)

You can also set the drawing width with the DrawWidth property.
Here’s 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.


Figure 18.12   Drawing dashed figures.

TIP:  You cannot use different drawing styles if the drawing width is not set to 1.





Previous
Table of Contents
Next






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.



Wyszukiwarka

Podobne podstrony:
604 606
596 599
fileQ83 3 br 604 info
604 606
599,14,artykul
59903
59915
59905
59916
599 603
Nuestro Circulo 599 JUEGA ALEKHINE
596 599
59902

więcej podobnych podstron