Graphics Methods VB Help

Now we can look atthe drawing methods of VISUalBasic, which are the following:
. .
• Print    Displays a string
• Line     Draws lines and boxes
• Circle   Draws circles and arcs
• Point.   Retrieves the Color value of a point
• PSel    Sets the color of a Point

The Print method has nothing to do with your printer; it draws text on a Form or a control. The Line and Circle methods accept many arguments that extend Visual Basic’s drawirig capabilities. For example, you can draw elaborate geometric shapes such as ellipses, filled shapes, and pie charts. The Point and PSet methods manipulate pixels and are used frequently in image processing applications and for drawing •curves, which must be plotted point by point. The last two methods are discussed in detail in the following chapter, Manipulating ColOrand Pixels with VISUal Basic.

Drawing Text

The simplest drawing method is the Print method, which draws text on a Form or a PictureBox control starting at the current point. The text is drawn in the control’s current font and size, and after-it’s drawn, the current point is moved to the end of the text. The following statement will display a string at the top of the Picture Box:

Picturel.Print ‘  Drawing with Visual Basic

It will also move the current point below the .string, as if you were printing on a multi-line TextBox control. The next string you’ll print on the same control will appear below’ the first one.

TextWidth, TextHeight Methods

Two methods commonly used to align text on a Form or a Picture Box control are TextWidth and TextHeight. These methods accept a string as argument and they re~ their. arguments’ width and height, respectively. The TextWidth and Text- Height properties apply to the objects that accept graphics methods, which are the Form object, Printer object, and the PictureBox control, and they report the length and width required to print a string on the printer at the current font.

VB6 at Work: The TxtPrint Project

The TxtPrint project (see Figure 6.8) demonstrat~s how to use the Print method along with the TextWidth and TextHeight properties to draw aligned text on a Form. To place a string in the middle of the form, you-must first calculate the . Form’s middle point:

Form1 .ScaleWidth/2, Form1.ScaleHeight/2

You must then subtract one-half of the text’s Width from the Xcoordinate and onehalf of the text’s height from the Ycoordinate. Setting the current point to these coordinates and then issuing the Print cominand centers the text on the Form:

Notice that the code shown here uses the ScaleWidth and ScaleHeight properties These two properties return the width and height of the” useful” area of the Form; the Form’s frame and title bar are excluded, The Width and Height properties report the external dimensions of the Form, includinq the frame and the title bar, Also, for the purposes of centering a stnng on a Fonn, the actual scale mode won’t make anv difference. The middJe point will be the same with any’scale, only the absolute values will differ.

You can open the FxfPrint application in Visual Basic’s editor to examine the rest of the code.

Drawing Lines and Shapes

The method for drawing lines is called Litle, and it has the following syntax:

Line[Step) (Xl. Yl) – [Step] (X2, Y2) ,[color], [BJ[F]

The arguments that appear in square brackets arc optional. The coordinates of the line’s starting point are Xl, Y1.,and X2. Y2 are the coordinates of the ending point. The followmg statement demonstrates the simplest form of the method

Line (Xl, Vl) – (X~, Y2)

The coordinates of the line’s endpoints are expressed in the units of the control’scoordinate system. The thickness of theline is determined b y the Drawt\Tidth property, and its style, by the DrawShJle property, whose settings are shown in Table 62. If the width of the line is greater than one’pixel, the settings Lthrough 4’are identical to setting O-that is, you can’t draw dashed or dotted lines that are thicker than one pixel.

The mea lUng at each property is ObVlOU:e:’x, cept for the last value: InsideSolid. When drawing WIth a line width larger than one pixel, Visual Basic splits the width of the line on both sides of the specified coordinates. Uyou set the DrawStyle property to 6 (Inside Solid), the shape (line, box, or circle) will be drawn entirely within the specified coordinates.

The following short program draws lines of different styles on the Picture! control:

Specifying Color

The ForeColor property of the Picture Box or Form determines the color of the shapes you draw. However, you can draw lines in different colors by specifying the optional argument color, available with the Line and Circle methods (the Circle method will be discussed shortly). The following statements show how the color argument is used.

All three examples draw a red line from (10, 10) to (100, 100), regardless of the current setting of the ForeColor property. The method’s color,argument can be any valid color expression. Normally, the line’s color is determined by the control’s ForeColor property, but the color argument bf the Line method overwrites the ForeColor property for the current line. If you draw another line, without – specifying a color argument, it will be drawn in the control’s Foreground color.

Using Relative Coordinates (The Step Option)

With the Step option of the Line method, you can define the second endpoint of the line relative to the first endpoint. In other words, the Step option defines a point not in terms of its coordinates, but in terms of its distance from the line’s first endpoint. The coordinates we have used so far are absolute because they specify a unique point on the screen as measured from the control’s upper-left comer. The coordinates following the Step option are relative. The difference between the two types of coordinates is their origin. Absolute coordinates are always measured froI the origin (Scale Left, Sealop), relative coordinates are measured from the current position, who  rever 15mighs be. The following statement draws a line that starts at point (100, 100) and exte ” S 100 units down and 200 units to the right’ from its starting point

Line (100, 100) – (30e, 2Gu).

The two numbers following the Step option aren’t the ~oordinates of the second endpoint, but its distance from the current point.You can also use the Step keyword in front of the Line method’s first argument, in which case the line’s starting  point is defined rel~tive to the current point (Currentx, CurrentY) . Relative coordinates are used frequently in dr•awing closed shapes, because it’s easier to define an endpoint by its distance from the previous one. Suppose you want to draw a box with dimensions 100 x 300, with its upper-left comer at the point (100, 400). You can draw this box in absolute coordinates, with the following commands:

For each of the previous commands, you calculate the absolute coordinates of each corner of the frame by adding the appropriate dimensions to the previous endpoint. It is much easier, however, to draw the same box with relative coordinates. Here’s how:

You must define the starting point in absolute coordinates, but for the remaining points, it makes sense to use relative coonimates. Notice that you don’t even need ‘to define the first endpoint of each side because it coincides with the second ‘endpoint of the previous side. The numbers following the Step option represent the distance of the next corner of the box from the previous one.

Posted on November 5, 2015 in Drawing with Visual Basic

Share the Story

Back to Top