|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Drawing TextYou can display text in forms and picture boxes with the Print method:
[object.]Print [ outputlist] [{ ; | , }]
The upper-left corner of the text you print appears at the location (CurrentX, CurrentY) (CurrentX and CurrentY are properties of forms or picture boxes). If you want to print multiple items on different lines, separate them with commas. If you want to print multiple items on the same line, separate them with semicolons. Lets see an example. Here, we draw text starting at the center of both a form and a picture box, Picture1 (note that to draw graphics from the Form Load event, you must set the form and picture boxs AutoRedraw property to True):
Private Sub Form_Load()
CurrentX = ScaleWidth / 2
CurrentY = ScaleHeight / 2
Print "Hello from Visual Basic"
Picture1.CurrentX = Picture1.ScaleWidth / 2
Picture1.CurrentY = Picture1.ScaleHeight / 2
Picture1.Print "Hello from Visual Basic"
End Sub
The result of the preceding code appears in Figure 18.2. Now were printing text in forms and picture boxes (well print on the printer later in this chapter).
Working With FontsYou have a lot of formatting options when working with text. In particular, you can use these font properties in forms and picture boxes:
For example, we set the font in a form to bold and the font size to 12 in a picture box this way (note that to draw graphics from the Form Load event, you must set the form and picture boxs AutoRedraw property to True):
Private Sub Form_Load()
CurrentX = ScaleWidth / 2
CurrentY = ScaleHeight / 2
FontBold = True
Print "Hello from Visual Basic"
Picture1.CurrentX = Picture1.ScaleWidth / 2
Picture1.CurrentY = Picture1.ScaleHeight / 2
Picture1.FontSize = 12
Picture1.Print "Hello from Visual Basic"
End Sub
The result of this code appears in Figure 18.3.
The Font Object You can also create a Font object that holds all the properties of a font; here are the Font objects properties (note that whereas the font property is FontStrikeThru, the Font object property is StrikeThrough, not StrikeThru):
To create a Font object, you dimension it as a new object of type StdFont. For example, heres how we install 24-point Arial as the font in a text box, using a Font object:
Private Sub Command1_Click()
Dim Font1 As New StdFont
Font1.Size = 24
Font1.Name = "Arial"
Set Text1.Font = Font1
End Sub
Which Fonts Are Available? You can also determine which fonts are available for either screen or printer by checking the Fonts property of the Visual Basic Printer and Screen objects. This property holds an array (0-based) of the available fonts names (note that this collection is not a collection of Font objects). Heres an example. To see all the fonts available on your display using Visual Basic, you can loop over all fonts in the Screen objectthe total number of fonts is stored in the FontCount propertyand display the font names in message boxes this way (note that this code may display a lot of message boxes):
Private Sub Command1_Click()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To Screen.FontCount
MsgBox Screen.Fonts(intLoopIndex)
Next intLoopIndex
End Sub
Drawing LinesYou draw lines in forms and picture boxes with the Line method: object.Line [Step] ( x1, y1) [Step] ( x2, y2), [color], [B][F] Here are the arguments you pass to Line:
Lets see an example. Here, well draw lines crisscrossing a form and a picture box, Picture1, when the user clicks a button:
Private Sub Command1_Click()
Line (0, 0)-(ScaleWidth, ScaleHeight)
Line (ScaleWidth, 0)-(0, ScaleHeight)
Picture1.Line (0, 0)-(Picture1.ScaleWidth, Picture1.ScaleHeight)
Picture1.Line (Picture1.ScaleWidth, 0)-(0, Picture1.ScaleHeight)
End Sub
The result of this code appears in Figure 18.4. Now were drawing lines in forms and picture boxes.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |