Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!: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 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
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
Begin this project by creating a new Standard EXE project, placing a Picture Box and a Command Button on the form. Dont worry about their exact position or size; well take care of these specifications in code. Leave all the control properties at their default values except for the Command Buttons Caption property, which should be set to E&xit. Place the single statement End in the Command Buttons Click procedure. Save the project and the form (I used the name CENTER for both). Next, display the Form_Load event procedure and add the code shown in Listing 12.1.
Listing 12.1 The Form_Load event procedure.
Private Sub Form_Load()
Make the form width and height equal to
half the screen dimensions.
Form1.Width = Screen.Width / 2
Form1.Height = Screen.Height / 2
Center the form on the screen.
Form1.Left = (Screen.Width - Form1.Width) / 2
Form1.TOP = (Screen.Height - Form1.Height) / 2
End Sub
When you start the program, the form displays centered on the screen with width and height equal to half the screen dimensions. The controls, however, remain in the positions they were given during program design. Setting the control positions is the next task.
To set the size and positions of the controls, I use an approach like the one used for the form: Specify each controls size and position in terms of its containers size. Where should we place this code? We want the controls to be positioned whenever the form size changes, as well as when the form is first displayed. The ideal place for this code is the Form_Resize event procedure, which is called when the form first displays and any time its size is changed (either by the user or in code). I wont bother explaining the code in this procedure (shown in Listing 12.2); Im sure youll be able to understand how it works from the comments. Dont forget to save the project as you work on it.
Listing 12.2 The Form_Resize event procedure.
Private Sub Form_Resize()
Make the Picture Box size equal to 90% of the form
width and 70% of its height.
Picture1.Width = Form1.ScaleWidth * 0.9
Picture1.Height = Form1.ScaleHeight * 0.7
Center the Picture Box left-to-right.
Picture1.Left = (Form1.ScaleWidth - Picture1.Width) / 2
Make the distance between the top of the Picture Box and
the form the same as the distance at the sides.
Picture1.TOP = Picture1.Left
Make the Command Button width 20% of the form width.
Command1.Width = Form1.ScaleWidth * 0.2
Make the Command Button height one third of the distance between the
bottom of the Picture Box and the edge of the form.
Command1.Height = (Form1.ScaleHeight - Picture1.ScaleHeight) / 3
Center the Command Button left-to-right.
Command1.Left = (Form1.ScaleWidth - Command1.Width) / 2
Center the Command Button vertically in the space between the
Picture Box and the edge of the form.
Command1.TOP = Form1.ScaleHeight - 0.66 * _
(Form1.ScaleHeight - (Picture1.TOP + Picture1.Height))
End Sub
Once the Form_Resize procedure is complete, run the program again. Youll see that the Picture Box and Command Button are precisely positioned in the form, as shown in Figure 12.2. If you change the window size by dragging a border, youll see that the controls maintain their relative sizes and positions.
TIP: Using Fixed-Sized Controls
Not all windows need to be resizable. In fact, dialog boxes that contain an array of Text Boxes, Option Buttons, and other controls are best left at a fixed size. You can accomplish this by setting the forms BorderStyle property to: 1 - Fixed Single, 3 - Fixed Dialog, or 4 -Fixed Toolwindow. Then you can design the form, placing the various controls in precise positions for the results you want, without worrying about the user messing things up by resizing the dialog box. You could also use the techniques presented here to adapt each controls size and position when the dialog box is resized. In my experience, thats a lot more work than its worth.
Figure 12.2 Automatically setting the size and position of controls.
Whos On First?
Before continuing our discussion of Visual Basics coordinate system, I think its wise to take a brief detour to explain the events that occur when a form is being loaded and displayed. This is an area that confuses many programmers, so if you get it straight now, you wont have to worry about it later.
Each form in a Visual Basic project exists in its own disk file. Showing a form on screen requires two steps:
1. Loading the form from disk into memory
2. Displaying the form
In a single-form project, the form is loaded and displayed when the program starts. For a multiple-form project, the designated startup form is loaded and displayed when the program starts. Other forms are loaded and displayed under program control.
You can use the Load statement to load a form without displaying it:
Load formname
A form will also be loaded if the program refers to any of its properties. Loading a form and displaying it are two different things, however. The Show method, shown here
formname.Show
is used to display a form. If the form has not already been loaded, Show loads it automatically. Rarely will you need to use the Load statement. I have found two instances, however, in which Load is useful:
If you want the forms Load event procedure to execute without the form being displayed, use the Load statement.
If the form is complex (that is, if it has many controls and/or performs complex processing in its Load event procedure), use the Load statement to load the form before you need to display it. Complex forms display more quickly if theyve been loaded ahead of time.
You can unload a form you no longer need if you want to free memory or to reset all of the forms properties to their original values. To unload a form, use the Unload statement:
Unload formname
If you want to remove a form from the screen without unloading it, use the Hide method:
formname.Hide
Forms also have a Paint event whose occurrence is related to a need to redraw some or all of the form. Figure 12.3 summarizes the relationships among the various methods, statements, and user actions that act on forms and the events that are triggered as a result. Youll see how the Paint event is used later in this chapter.
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. Read EarthWeb's privacy statement.
Wyszukiwarka
Podobne podstrony:
Wykład 1 (12 03 2011) ESI 07 12 03 pra12 03 07zajecia 12 03 10Kształcenie ruchowe – ćwiczenia nr 4 (12 03 12r )TI 98 12 03 T pl(1)Meta 12 03 201212 03 11 A12 03 Roboty betoniarskie i zbrojarskieGeo fiz wykład 12 03 201312 03 11 Rwięcej podobnych podstron