Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Drawing Text

You 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.

Let’s 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 box’s 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 we’re printing text in forms and picture boxes (we’ll print on the printer later in this chapter).


Figure 18.2   Drawing text in a form and picture box.


TIP:  You can format text when you print it to forms, picture boxes, or the Printer object by determining its width and height, and you do that with the TextWidth and TextHeight methods.

Working With Fonts

You have a lot of formatting options when working with text. In particular, you can use these font properties in forms and picture boxes:

  FontBold
  FontItalic
  FontName
  FontSize
  FontStrikeThru
  FontTransparent
  FontUnderline

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 box’s 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.


Figure 18.3   Setting font properties in Visual Basic.

The Font Object

You can also create a Font object that holds all the properties of a font; here are the Font object’s properties (note that whereas the font property is FontStrikeThru, the Font object property is StrikeThrough, not StrikeThru):

  Bold
  Italic
  Name
  Size
  StrikeThrough
  Underline
  Weight

To create a Font object, you dimension it as a new object of type StdFont. For example, here’s 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 font’s names (note that this collection is not a collection of Font objects).

Here’s an example. To see all the fonts available on your display using Visual Basic, you can loop over all fonts in the Screen object—the total number of fonts is stored in the FontCount property—and 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


TIP:  You can format text when you print it to forms, picture boxes, or the Printer object by determining its width and height, and you do that with the TextWidth and TextHeight methods.

Drawing Lines

You 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:

  Step—Keyword specifying that the starting point coordinates are relative to the current graphics position given by the CurrentX and CurrentY properties.
  x1, y1Single values indicating the coordinates of the starting point for the line or rectangle. The ScaleMode property determines the unit of measure used. If omitted, the line begins at the position indicated by CurrentX and CurrentY.
  Step—Keyword specifying that the end point coordinates are relative to the line starting point.
  x2, y2Single values indicating the coordinates of the end point for the line being drawn.
  colorLong integer value indicating the RGB color used to draw the line. If omitted, the ForeColor property setting is used. You can use the RGB function or QBColor function to specify the color.
  B—If included, causes a box to be drawn using the coordinates to specify opposite corners of the box.
  F—If the B option is used, the F option specifies that the box is filled with the same color used to draw the box. You cannot use F without B. If B is used without F, the box is filled with the current FillColor and FillStyle. The default value for FillStyle is transparent.

Let’s see an example. Here, we’ll 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 we’re drawing lines in forms and picture boxes.


Figure 18.4   Drawing lines in forms and picture boxes.


Previous Table of Contents Next


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.