Visual Basic provides two basic methods for drawing shapes on controls and a method for displaying text:
• Line Draws lines and boxes
• Circle Draws circles and ellipses
• Print Displa.ys text strings
(A fourth method, Point, turns individual pixels on and off, and we’llexplore thismethod in the following chapter.)
Before you dra w anything, you must know the dimensions of the control you’re about to draw on and the units it uses. To do this, you must understand coordinates and coordinate systems and how they are used in drawing.
Coordinates describe the position of the individual pixels on the screen or of. the points on a piece of paper in the printer. The coordinate system issimilar to ) a city map. Each square on the map has its own unique address: a combination of a column: and a row number. The column number is the vertical coordinate, or Ycoofdinate. The row number is the horizontal coordinate, OrX coordinate. Any point on the Form can be identified by its X and Ycoordinates, and-we refer to it as the point at coordinates (X,Y) or simply the point (X, Y).An example is shown in Figure 6.6. The values along the x-axis in the coordinate system of the figure go from 0 to 100, and the values along the y-axis go from -so to so. Any pair of numbers in the ranges 0 to 100 and ~SOto SOspecifies a point on the Form.
The point with the smallest coordinates is the origin of the coordinate system. In Visual &Sic, the origin of the coordinate system is the upper-left comer of the control or Form. The Xcoordinates increase to the right and the Ycoordiriates increase downward. Each coordinate is a number and it mayor may not correspond to a meaningful unit. For example, the letter and number coordinates on a city map don’t correspond to meaningful units; they are arbitrary. The coordinates on a topological map, though, correspond to physical distances (e.g., kilometers, miles). It all depends on the intended application.
For example, if you want to draw a plan for your new house, you need to use a coordinate system in inches or centimeters so that there will be some relation between units and the objects you draw. If you’re going to draw some nice geometrical shapes, any coordinate system will do. Finally, if you’re going to display and process images, you’d want a coordinate system that uses pixels as units. Visual Basic supports several coordinate systems, including a user-defined coordinate system that lets users set up their own. units. If you’re familiar with computer graphics in older languages or operating systems, you’d probably expect that the most common coordinate system to be based on pixels. Actually, pixels aren’t the best units for all applications. If you use pixels to address a control’s contents, you’re tied to a particular resolution. If the’ monitor’s resolution is increased, your designs are going to look different. Even the aspect ratio may not be the same, and the circles will become ellipses.
Visual Basic’s default coordinate system uses a unit called iunp, which equals 1/20 of a point. A point is a typographical measure of unit; there are 72 points in an inch and 1,440 twips in an inch. The twip is a precise unit of measurement, probably more precise than we need today. It does, however, allow us to draw shapes that will look good even when printed on a 1,200 dpi laser printer. But . because twips aren’t convenient in all situations, Visual Basic provides the eight coordinate systems
To change the default coordinate system, assign the appropriate value to the ScaleMode property. If you set ScaleMode to Inches; distances on the control must be specified in inches. In tlus case, two points that are one unit apart are one inch from each other. You can also specify decimal distances such as 0.1, which corresponds to 1/10 of an inch. l hanging the ScaleMode property doesn’t resize or otherwise affect the control. It simply changes the density of the grid you use to address the points on the control.
If none of the pre-defined coordinate systems suit your needs, you can create your own. Suppose you want to implement a game board with 12 squares along the x-axis and eight squar.es along the y-axis. The pieces o.f the game can only rest on a square of the grid, so you don’t need the precision of units sur! I ‘1″ twips or’ pixels. You can set up a coordinate system that extends from 0 to 1 J in I’ll:’ horizont<> 1direction and from 0 to 7 in the vertical direction. If the game board IS 32 )( 32 squaresJ you set up a coordinate system that extends 32 units in both directions. The size of the PictureBox control doesn’t change. There is, however. enol 1~ll space for 32 x 32 squares, and each square is addressed with integer coordinates to set up a user-defined coordinate system, you can use the Scale method or the Scalerelated properties, which are explained next.