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.

the picture box. The TextWidth method is essential when centering text, as illustrated in the<br />

following example.<br />

EXAMPLE 5<br />

Write an event procedure to display the phrase “Th-that’s all Folks!” centered <strong>and</strong> double underlined in a<br />

picture box with x values ranging from 0 to 6 <strong>and</strong> y values ranging from 0 to 4.<br />

SOLUTION:<br />

Centering text requires knowing the coordinates of the center of the picture box, which for the given<br />

ranges will be the point (3, 2). Next, we need the width <strong>and</strong> height of the text being centered. These values<br />

are available using the TextWidth <strong>and</strong> TextHeight methods. The text cursor needs to start with a CurrentX<br />

that is half the text’s width to the left of center <strong>and</strong> a CurrentY that is half the text’s height above<br />

center. The first underline can be placed at half the text’s height below center. The additional distance<br />

down to the second underline should be in proportion to the height of the text. We decided after some<br />

experimenting to use a proportion of 1/6th.<br />

Private Sub cmdDraw_Click()<br />

‘Center <strong>and</strong> double underline a phrase<br />

Dim xCenter As Single, yCenter As Single, phrase as String<br />

Dim w As Single, h As Single, leftEdge As Single, rightEdge As Single<br />

Dim ul1Pos As Single, ul2Pos As Single<br />

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

picOutput.Cls<br />

xCenter = 3<br />

yCenter = 2<br />

phrase = “Th-that’s all Folks!”<br />

w = picOutput.TextWidth(phrase)<br />

h = picOutput.TextHeight(“ ”)<br />

picOutput.CurrentX = xCenter - w / 2<br />

picOutput.CurrentY = yCenter - h / 2<br />

picOutput.Print phrase<br />

leftEdge = xCenter - w / 2<br />

rightEdge = xCenter + w / 2<br />

ul1Pos = yCenter + h / 2<br />

ul2Pos = ul1Pos + h / 6<br />

picOutput.Line (leftEdge, ul1Pos)-(rightEdge, ul1Pos)<br />

picOutput.Line (leftEdge, ul2Pos)-(rightEdge, ul2Pos)<br />

End Sub<br />

[Run, <strong>and</strong> then click the comm<strong>and</strong> button. The resulting picture box follows.]<br />

COMMENTS<br />

1. In Examples 1 through 4, examples that produce graphs, the range of numbers<br />

on the axes extended from a negative number to a positive number. Actually,<br />

any value of a, b, c, <strong>and</strong> d can be used in a Scale method. In certain cases, however,<br />

you will not be able to display one or both of the axes on the screen. (For<br />

instance, after picOutput.Scale (1, 10)–(10, –1) has been executed, the y axis<br />

cannot be displayed.)<br />

Introduction to Graphics 265

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

Saved successfully!

Ooh no, something went wrong!