HOWTO: Use the DrawText API Call from Visual Basic

Last reviewed: September 30, 1997
Article ID: Q154517
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 32-bit only, for Windows, version 4.0

SUMMARY

The DrawText API gives users the ability to write text to a Device context giving various alignment and word-wrapping features. This article provides a code sample showing how to achieve these effects under the 32-bit Edition of Visual Basic 4.0.

MORE INFORMATION

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a Command button to Form1.

  3. Place the following code in the General Declarations section of Form1:

          Option Explicit
    

          Private Declare Function DrawText Lib "user32" Alias "DrawTextA" _
    
            (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, _
            lpRect As RECT, ByVal wFormat As Long) As Long
    
          Private Const DT_BOTTOM = &H8
          Private Const DT_CENTER = &H1
          Private Const DT_LEFT = &H0
          Private Const DT_RIGHT = &H2
          Private Const DT_TOP = &H0
          Private Const DT_VCENTER = &H4
          Private Const DT_WORDBREAK = &H10
    
          Private Type RECT
             Left As Long
             Top As Long
             Right As Long
             Bottom As Long
          End Type
    
          Private Sub Command1_Click()
            Dim lSuccess As Long
            Dim sPrintText As String
            Dim MyRect As RECT
            Form1.Font.Size = 12
            Form1.ScaleMode = vbPixels
            MyRect.Left = 0
            MyRect.Right = Form1.ScaleWidth
            MyRect.Top = 20
            MyRect.Bottom = 60
            sPrintText = "Print this text"
            lSuccess = DrawText(Form1.hdc, sPrintText, Len(sPrintText), _
            MyRect, DT_CENTER or DT_WORDBREAK)
          End Sub
    
    

  4. Press the F5 key to run the project, and press the Command button. Text should appear in the middle of the form and be word-wrapped. If you Resize the form and run the project again, you will note that the text still appears in the middle of the form on the horizontal axis.

Following is a list of all the constants that can be used with the DrawText API to give the various effects documented in the Win32 SDK:

   Global Const DT_TOP = &H00000000
   Global Const DT_LEFT = &H00000000
   Global Const DT_CENTER = &H00000001
   Global Const DT_RIGHT = &H00000002
   Global Const DT_VCENTER = &H00000004
   Global Const DT_BOTTOM = &H00000008
   Global Const DT_WORDBREAK = &H00000010
   Global Const DT_SINGLELINE = &H00000020
   Global Const DT_EXPANDTABS = &H00000040
   Global Const DT_TABSTOP = &H00000080
   Global Const DT_NOCLIP = &H00000100
   Global Const DT_EXTERNALLEADING = &H00000200
   Global Const DT_CALCRECT = &H00000400
   Global Const DT_NOPREFIX = &H00000800
   Global Const DT_INTERNAL = &H00001000
   Global Const DT_EDITCONTROL = &H00002000
   Global Const DT_PATH_ELLIPSIS = &H00004000
   Global Const DT_END_ELLIPSIS = &H00008000
   Global Const DT_MODIFYSTRING = &H00010000
   Global Const DT_RTLREADING = &H00020000
   Global Const DT_WORD_ELLIPSIS = &H00040000

REFERENCES

The Win32 SDK has a comprehensive summary of Drawtext and other Drawing and Text functions.

Keywords          : vb432 VB4WIN vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS:4.0
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.