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 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Now let’s move on to the demonstration program. This program displays text in a Picture Box. First, vertical and horizontal lines are drawn to mark the center of the box. Two sets of option buttons allow you to select both vertical and horizontal text alignment. All alignment is with respect to the box’s center, marked by the two lines.

Start a new standard EXE project. On the form, add a Picture Box, a Command Button, and two Frame controls. On each of the Frame controls, create a control array of three Option Buttons. The program is shown executing in Figure 27.1.


Figure 27.1  Using a Windows API procedure to align text.

The object properties are shown in Listing 27.1; the form’s code is shown in Listing 27.2.

Listing 27.1 Objects and properties in ALIGNTXT.FRM.

Begin VB.Form frmTxtAlign
   BorderStyle   =   1  ‘Fixed Single
   Caption       =   “Windows API Demonstration - SetTextAlign()”
   Begin VB.CommandButton cmdExit
      Caption       =   “E&xit”
   End
   Begin VB.PictureBox Picture1
      DrawStyle     =   3  ‘Dash-Dot
      BeginProperty Font
         name          =   “Times New Roman”
         size          =   15.75
      EndProperty
   End
   Begin VB.Frame Frame2
      Caption       =   “Horizontal alignment”
      Begin VB.OptionButton HorizAlign
         Caption       =   “&Left”
         Index         =   0
      End
      Begin VB.OptionButton HorizAlign
         Caption       =   “&Center”
         Index         =   1
      End
      Begin VB.OptionButton HorizAlign
         Caption       =   “&Right”
         Index         =   2
      End
   End
   Begin VB.Frame Frame1
      Caption       =   “Vertical alignment”
      Begin VB.OptionButton VertAlign
         Caption       =   “&Top”
         Index         =   0
      End
      Begin VB.OptionButton VertAlign
         Caption       =   “&Baseline”
         Index         =   1
      End
      Begin VB.OptionButton VertAlign
         Caption       =   “Bo&ttom”
         Index         =   2
      End
   End
End

Listing 27.2 Code in ALIGNTXT.FRM.

Option Explicit

Const TA_LEFT = 0
Const TA_RIGHT = 2
Const TA_CENTER = 6
Const TA_TOP = 0
Const TA_BOTTOM = 8
Const TA_BASELINE = 24

Const MESSAGE = “Visual Basic Blue Book”

Private Declare Sub SetTextAlign Lib “gdi32” (ByVal hDC As Long, _
   ByVal wFlags As Long)

Private Sub cmdExit_Click()

End

End Sub

Private Sub Form_Load()

+‘ Set default option button.

HorizAlign(0).VALUE = True
VertAlign(0).VALUE = True

End Sub

Private Sub HorizAlign_Click(Index As Integer)

‘ If the alignment option has been changed,
‘ repaint the Picture Box.

Picture1_Paint

End Sub

Private Sub Picture1_Paint()

Dim wFlags As Long

‘ Clear the picture box.
Picture1.Cls

‘ Draw centered vertical and horizontal lines.
Picture1.Line (0, Picture1.ScaleHeight / 2)-Step(Picture1.ScaleWidth, 0)
Picture1.Line (Picture1.ScaleWidth / 2, 0)-Step(0, Picture1.ScaleHeight)

‘ Place the current position at the intersection of the lines.
Picture1.CurrentY = Picture1.ScaleHeight / 2
Picture1.CurrentX = Picture1.ScaleWidth / 2

‘ Set wFlags to reflect the alignment options selected.
If HorizAlign(0).VALUE Then wFlags = TA_LEFT
If HorizAlign(1).VALUE Then wFlags = TA_CENTER
If HorizAlign(2).VALUE Then wFlags = TA_RIGHT

If VertAlign(0).VALUE Then wFlags = wFlags Or TA_TOP
If VertAlign(1).VALUE Then wFlags = wFlags Or TA_BASELINE
If VertAlign(2).VALUE Then wFlags = wFlags Or TA_BOTTOM

‘ Set the new alignment.
Call SetTextAlign(Picture1.hDC, wFlags)

‘ Display the text.
Picture1.Print MESSAGE

End Sub

Private Sub VertAlign_Click(Index As Integer)

‘ If the alignment option has been changed,
‘ repaint the picture box.

Picture1_Paint

End Sub

Note how we draw the lines in the box. First, the Picture Box’s DrawStyle property is set to 3-Dash-Dot, giving us lines with a dot-dash pattern. Then, in code, we use the Line method to draw the lines, obtaining the needed coordinates from the control’s ScaleHeight and ScaleWidth properties:

Picture1.Line (0, Picture1.ScaleHeight / 2)-Step(Picture1.ScaleWidth, 0)
Picture1.Line (Picture1.ScaleWidth / 2, 0)-Step(0, Picture1.ScaleHeight)

When you use this method to set text alignment, be aware that the alignment you set with SetTextAlign is guaranteed to be in effect for only a single Print method. Suppose, for example, you change text alignment and then execute these statements:

PictureBox.Print “text1”
PictureBox.Print “text2”

The specified alignment may not be used for the second message. You must call SetTextAlign before each and every Print method. Because Visual Basic treats multiple arguments to a Print method as two distinct Print methods, the following still may not work:

PictureBox.Print “text1”;“text2”

Avoid the potential problem by concatenating the strings before executing Print:

PictureBox.Print “text1” & “text2”

Using Callbacks

A few of the Windows API functions use callbacks. A callback is used when the API function requires some assistance from your program. You write a function in your Visual Basic program, called, appropriately enough, a callback function, that performs the actions required by the API function. Your program calls the API function, which, in turn, calls the callback function as needed. These are the four steps for using a callback:

1.  The program calls the API function.
2.  The API function performs some processing and then calls the callback function.
3.  The callback function performs its task and returns execution to the API function.
4.  The API function returns execution to your Visual Basic program. In some cases, execution passes back and forth multiple times between the API function and the callback function before the API function terminates. The process is outlined in Figure 27.2.


Figure 27.2  Operation of an API function that uses a callback.

How does the API function know about the callback function? You tell it, that’s how. One of the arguments that your program must pass to the API function is the address of the callback function. This is sometimes called a function pointer, as you will know if you’ve worked with C or C++. The API function uses this address to call the callback function when it needs to. You use the AddressOf operator to provide the address of a function defined in your Visual Basic program.

For example, EnumWindows is one of the API functions that requires a callback. Its purpose is to loop through all of the top-level windows on the screen, permitting you to obtain information about, or perform some action with, some or all of the windows. Its prototype in a Visual Basic program is:

Declare Function EnumWindows lib “user32” _
    (ByVal lpEnumFunc as Long, _
     ByVal lParam as Long ) As Long


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. Read EarthWeb's privacy statement.