|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Defining Custom CoordinatesIf Visual Basics assortment of coordinate units doesnt suit you, define your own. A custom coordinate system can include moving the origin to another location, a useful technique for certain applications. For example, a program that draws maps could set the graphical coordinate units to a meaningful value, such as miles or latitude and longitude. You can also flip the Y axis so positive values move upward, making it easier to use the Cartesian coordinates commonly used in graphs. A custom coordinate system affects not only graphics operations, but also the placement of controls in a container. To define a custom coordinate systemwhich is possible only for Form, Picture Box, and Printer objectsuse the Scale method. The syntax is objectname.Scale (left, top)-(right, bottom) where (left, top) are the new coordinates of the left and top of the object, and (right, bottom) are the new coordinates for the right and bottom of the object. For example Form1.Scale (0, 0)-(100, 100) sets a coordinate system where the top-left corner of the object has coordinates (0, 0) and the lower-right corner has coordinates (100, 100). This example does not move the origin, but only changes the measurement unit to be 1/100 of the object dimension. In contrast, the statement Form1.Scale (0, 100)-(100, 0) sets the same measurement units, then moves the origin to the lower-left corner of the object and inverts the Y axis so positive values move upward. When you use Scale to set custom coordinates, Visual Basic automatically changes the objects ScaleMode property to 0. As you saw in Table 12.1, this indicates that a custom coordinate system is in effect. Applying the Scale method changes the objects ScaleWidth, ScaleHeight, ScaleLeft, and ScaleTop properties to reflect the new coordinates. For instance, after executing the previous Scale method, the objects ScaleWidth property would be 100 and its ScaleHeight property would be 100. You can set these properties individually if you want to modify only part of the objects coordinate system. Thus, the statement object.ScaleWidth = 100 results in the objects horizontal measurement unit being set to 1/100 of its width, while the location of the origin and the vertical measurement unit remain unchanged. Executing the Scale method with no arguments, like this object.Scale will reset the coordinate system to the default (twips with the 0, 0 point at the top left). You must be aware that the Scale method defines its units based on the objects size at the time the method is executed. Subsequent changes to the objects size are not automatically taken into account. Well see how this works in the custom coordinate demonstration program. This program uses the Circle method in a For...Next loop to draw a series of circles on a form. Youll learn the details of the Circle method later in the chapteryou neednt worry about it now. The program consists of a form without controls. Leave all of the forms properties at their default values and save your work, calling both the form and the project SCALE. Next, place the code shown in Listing 12.3 in the forms Paint event procedure. Listing 12.3 The Paint event procedure. Private Sub Form_Paint() Dim i As Integer Cls For i = 1 To 5 Circle (150 * i, 100 * i), 50 * i Next i End Sub The Circle method draws a circle with its center at the specified coordinates (the values in the parentheses) and with the specified radius. When you execute the program, it will look more or less like Figure 12.4. I say more or less, because the appearance may vary slightly if you made your form a different size than I did or if your display hardware is different from mine.
After running the program and seeing what the display looks like, add the following statement to the forms Load event procedure: Scale (0, 1000)-(1000, 0) Run the program again, and youll see a quite different display, as shown in Figure 12.5.
Looking at the new display, youll see that not only are the circles larger (because the coordinate unit we specified is much larger than a twip), but the circles climb from the bottom of the form rather than descending from the top (because we inverted the Y axis). If you change the size of the form while the program is running, the circles stay the same sizethey do not grow or shrink along with the form. Now lets try one more thing. Copy the Scale (0, 1000)-(1000, 0) statement and paste it in the Paint event procedure, just after the Cls statement. When you run the program and increase the size of the form by dragging its border, the circles are redrawn to keep the same size relative to the form. By resetting the forms coordinate system each time it is painted, we ensure that the coordinate system units are always defined relative to the forms current sizenot according to its original size.
|
![]() |
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. |