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.
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.
Figure 18.9 Drawing freehand with the mouse.
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 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.
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.
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 boxs drawing color (and if you didnt 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:
VbFSSolid0; solid
VbFSTransparent1 (the default); transparent
VbHorizontalLine2; horizontal line
VbVerticalLine3; vertical line
VbUpwardDiagonal4; upward diagonal
VbDownwardDiagonal5; downward diagonal
VbCross6; cross
VbDiagonalCross7; 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. 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:
vbSolid1 (the default); solid (the border is centered on the edge of the shape)
vbDash2; dash
vbDot3; dot
vbDashDot4; dash-dot
vbDashDotDot5; dash-dot-dot
vbInvisible5; invisible
vbInsideSolid6; 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.
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.
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 606596 599fileQ83 3 br 604 info604 606599,14,artykul599 03599 15599 05599 16599 603Nuestro Circulo 599 JUEGA ALEKHINE596 599599 02więcej podobnych podstron