Visual Basic Concepts
You set the coordinate system for a particular object (form or control) using the object’s scale properties and the Scale method. You can use the coordinate system in one of three different ways:
Changing the scale of the coordinate system can make it easier to size and position graphics on a form. For example, an application that creates bar charts in a picture box can change the coordinate system to divide the control into four columns, each representing a bar in the chart. The following sections explain how to set default, standard, and custom scales to change the coordinate system.
Every form and picture box has several scale properties (ScaleLeft, ScaleTop, ScaleWidth, ScaleHeight, and ScaleMode) and one method (Scale) you can use to define the coordinate system. The default scale for objects in Visual Basic places the coordinate (0,0) at the upper-left corner of the object. The default scale uses twips.
If you want to return to the default scale, use the Scale method with no arguments.
Instead of defining units directly, you can define them in terms of a standard scale by setting the ScaleMode property to one of the settings shown in the following table.
ScaleMode setting | Description |
0 | User-defined. If you set ScaleWidth, ScaleHeight, ScaleTop, or ScaleLeft directly, the ScaleMode property is automatically set to 0. |
1 | Twips. This is the default scale. There are 1,440 twips to one inch. |
2 | Points. There are 72 points to one inch. |
3 | Pixels. A pixel is the smallest unit of resolution on the monitor or printer. The number of pixels per inch depends on the resolution of the device. |
4 | Characters. When printed, a character is 1/6 of an inch high and 1/12 of an inch wide. |
5 | Inches. |
6 | Millimeters. |
7 | Centimeters. |
All of the modes in the table, except for 0 and 3, refer to printed lengths. For example, an item that is two units long when ScaleMode is set to 7 is two centimeters long when printed.
' Set scale to inches for this form.
ScaleMode = 5
' Set scale to pixels for picPicture1.
picPicture1.ScaleMode = 3
Setting a value for ScaleMode causes Visual Basic to redefine ScaleWidth and ScaleHeight so that they are consistent with the new scale. ScaleTop and ScaleLeft are then set to 0. Directly setting ScaleWidth, ScaleHeight, ScaleTop, or ScaleLeft automatically sets ScaleMode to 0.
You can use an object’s ScaleLeft, ScaleTop, ScaleWidth, and ScaleHeight properties to create a custom scale. Unlike the Scale method, these properties can be used either to set the scale or to get information about the current scale of the coordinate system.
The ScaleLeft and ScaleTop properties assign numeric values to the upper-left corner of an object. For example, these statements set the value of the upper-left corner for the current form and upper-left corner for a picture box named picArena.
ScaleLeft = 100
ScaleTop = 100
picArena.ScaleLeft = 100
picArena.ScaleTop = 100
These scale values are shown in Figure 12.4.
Figure 12.4 The ScaleLeft and ScaleTop properties for a form and a control
These statements define the upper-left corner as (100, 100). Although the statements don’t directly change the size or position of these objects, they alter the effect of subsequent statements. For example, a subsequent statement that sets a control’s Top property to 100 places the object at the very top of its container.
The ScaleWidth and ScaleHeight properties define units in terms of the current width and height of the drawing area. For example:
ScaleWidth = 1000
ScaleHeight = 500
These statements define a horizontal unit as 1/1,000 of the current internal width of the form and a vertical unit as 1/500 of the current internal height of the form. If the form is later resized, the units remain the same.
Note ScaleWidth and ScaleHeight define units in terms of the internal dimensions of the object; these dimensions do not include the border thickness or the height of the menu or caption. Thus, ScaleWidth and ScaleHeight always refer to the amount of room available inside the object. The distinction between internal and external dimensions (specified by Width and Height) is particularly important with forms, which can have a thick border. The units can also differ: Width and Height are always expressed in terms of the container’s coordinate system; ScaleWidth and ScaleHeight determine the coordinate system of the object itself.
All four of these scale properties can include fractions and they can also be negative numbers. Negative settings for the ScaleWidth and ScaleHeight properties change the orientation of the coordinate system.
The scale shown in Figure 12.5 has ScaleLeft, ScaleTop, ScaleWidth, and Scale Height all set to 100.
Figure 12.5 Scale running from (100, 100) to (200, 200)
A more efficient way to change the coordinate system, other than setting individual properties, is to use the Scale method. You specify a custom scale using this syntax:
[object.]Scale (x1, y1) – (x2, y2)
The values of x1 and y1 determine the settings of the ScaleLeft and ScaleTop properties. The differences between the two x-coordinates and the two y-coordinates determine the settings of ScaleWidth and ScaleHeight, respectively. For example, suppose you set the coordinate system for a form by setting end points (100, 100) and (200, 200):
Scale (100, 100)-(200, 200)
This statement defines the form as 100 units wide and 100 units high. With this scale in place, the following statement moves a shape control one-fifth of the way across the form:
shpMover.Left = shpMover.Left + 20
Specifying a value of x1 > x2 or y1 > y2 has the same effect as setting ScaleWidth or ScaleHeight to a negative value.