![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
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.
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:
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:
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.
|
![]() |
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. |