Re: Formated Text?

From: Larry Serflaten (serflaten_at_usinternet.com)
Date: 03/02/04


Date: Tue, 2 Mar 2004 12:33:08 -0600


"Amrit" <cadd@wlink.com.np> wrote in message
> Hi all
> How to Print Formated Text in Picture Box?

Here is a demo to print to the Form. A picture box is similar.
(It has an hDC property).

To try it out, add a class to a new project called TextFormatter.
Then paste in the code in their respective modules and try it out.
For more info on what the format commands do, see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dx8_vb/directx_vb/Graphics/Reference/VB/D3DX/Enums/CONST_DTFLAGS.
asp
(make sure link is not broken by word wrap)

HTH
LFS

' [ TextFormatter class ]
Option Explicit

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Public Enum CONST_DTFLAGS
    DT_LEFT = 0
    DT_TOP = 0
    DT_CENTER = 1
    DT_RIGHT = 2
    DT_VCENTER = 4
    DT_BOTTOM = 8
    DT_WORDBREAK = 16 '(&H10)
    DT_SINGLELINE = 32 '(&H20)
    DT_EXPANDTABS = 64 '(&H40)
    DT_TABSTOP = 128 '(&H80)
    DT_NOCLIP = 256 '(&H100)
    DT_EXTERNALLEADING = 512 '(&H200)
    DT_CALCRECT = 1024 '(&H400)
    DT_NOPREFIX = 2048 '(&H800)
    DT_INTERNAL = 4096 '(&H1000)
    DT_EDITCONTROL = 8192 '(&H2000)
    DT_PATH_ELLIPSIS = 16384 '(&H4000)
    DT_END_ELLIPSIS = 32768 '(&H8000)
    DT_MODIFYSTRING = 65536 '(&H10000)
    DT_RTLREADING = 131072 '(&H20000)
    DT_WORD_ELLIPSIS = 262144 '(&H40000)
    DT_NOFULLWIDTHCHARBREAK = 524288 '(&H80000)
    DT_HIDEPREFIX = 1048576 '(&H100000)
    DT_PREFIXONLY = 2097152 '(&H200000)
End Enum

Private Declare Sub DrawText Lib "user32" Alias "DrawTextA" ( _
        ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, _
        lpRect As RECT, ByVal wFormat As Long)

Dim MyRect As RECT

Public Sub Move(ByVal Left As Long, ByVal Top As Long, _
                ByVal Width As Long, ByVal Height As Long)
' This routine is to help make the process more 'familiar'
  With MyRect
    .Left = Left
    .Top = Top
    .Right = Width + Left
    .Bottom = Height + Top
  End With

End Sub

Public Sub DrawString(ByVal hdc As Long, Text As String, Format As CONST_DTFLAGS)
  DrawText hdc, Text, Len(Text), MyRect, Format
End Sub

Private Sub Class_Initialize()
  Move 0, 0, 100, 100
End Sub

' [ Form1 ]
Option Explicit

Private Sub Form_Load()
Dim Text As String
Dim TF As New TextFormatter

   Move Left, Top, 8000, 7000
   ' API always uses Pixels
   ScaleMode = vbPixels
   ' To make it stay on the form
   AutoRedraw = True

   ' Just some text to test
   Text = " Blabity blab hapablab bablabity-blab blab" & Space(1000)
   Mid(Text, 44) = Text

   ' Drawing a border on the form to see exactly how it wraps
   Line (6, 10)-Step(155, 405), vbWhite, B
   ' Use this to define the text area (in pixels)
   TF.Move 8, 10, 150, 405
   ' Check MSDN for what the format commands do
   TF.DrawString Me.hdc, Text, DT_WORDBREAK

   ForeColor = vbBlue
   Font.Bold = True

   Line (168, 10)-Step(155, 405), vbWhite, B
   TF.Move 170, 10, 150, 405
   TF.DrawString Me.hdc, Text, DT_CENTER Or DT_WORDBREAK

   ForeColor = vbRed
   Font.Size = 14

   Line (332, 10)-Step(155, 405), vbWhite, B
   TF.Move 330, 10, 150, 405
   TF.DrawString Me.hdc, Text, DT_RIGHT Or DT_WORDBREAK

End Sub



Relevant Pages