19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

264 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

text appropriately in the picture box is essential to good-looking graphs. A picture box has<br />

two properties, CurrentX <strong>and</strong> CurrentY, <strong>and</strong> two methods, TextHeight <strong>and</strong> TextWidth, that<br />

allow us to precisely position text alongside graphics.<br />

The properties CurrentX <strong>and</strong> CurrentY record the precise horizontal <strong>and</strong> vertical location<br />

at which the next character of text will be printed. By assigning appropriate values to<br />

these properties before executing a Print method, text can be positioned very precisely in the<br />

picture box. In the following event procedure, the coordinates of the right end of the tick<br />

mark are (x, y) = (.3, 3). As a first attempt at labeling a tick mark on the y axis, the CurrentX<br />

<strong>and</strong> CurrentY properties are set to these coordinates. The results are shown in Figure 9-7(a).<br />

Private Sub cmdDraw_Click()<br />

picOutput.Cls picOutput.Scale (-4, 4)-(4, -4)<br />

picOutput.Line (-4, 0)-(4, 0) ‘Draw x-axis<br />

picOutput.Line (0, -4)-(0, 4) ‘Draw y-axis<br />

picOutput.Line (-.3, 3)-(.3, 3) ‘Draw tick mark<br />

picOutput.CurrentX = .3 ‘Right end of tick mark<br />

picOutput.CurrentY = 3 ‘Same vertical position as tick mark<br />

picOutput.Print “y=3”<br />

End Sub<br />

‘Label for tick mark<br />

FIGURE 9-7 Placing Labels: (a) First Attempt <strong>and</strong> (b) Second Attempt<br />

Note that the top of the text is even with the tick mark. This reflects the fact that the<br />

value of the CurrentY property used by <strong>Visual</strong> <strong>Basic</strong> is the location for the top of the character<br />

cursor. Ideally, the text should be moved up so that the tick mark aligns with the middle<br />

of the text. To do this, the value of the CurrentY property needs to be increased by<br />

one-half the height of the text. The following statement assigns a corrected value to the CurrentY<br />

property by using the TextHeight method to obtain the height of the text being used as<br />

the tick mark label. (Since c < d in the scale method, TextHeight returns (–1) � [height of<br />

text].)<br />

picOutput.CurrentY = 3 - picOutput.TextHeight(“y=3”) / 2<br />

The result of using this corrected value for CurrentY is shown in Figure 9-7(b).<br />

When the TextHeight method is used, all characters have the same height. Thus the<br />

height of a string can be obtained by asking for the height of any single character. The following<br />

procedure uses the TextHeight method with a space character to center the text cursor<br />

at the requested graphic point.<br />

Private Sub PositionText(x As Single, y As Single)<br />

‘Center text cursor at the point (x, y)<br />

picOutput.CurrentX = x<br />

picOutput.CurrentY = y – picOutput.TextHeight(“ ”) / 2<br />

End Sub<br />

Another useful picture box method is TextWidth. Whereas the Len function returns the<br />

number of characters in a string, the TextWidth method considers the varying widths of characters<br />

<strong>and</strong> returns the physical width of the entire string in the units of the current scale for

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!