Public Const DT_BOTTOM = &H8
Public Const DT_CALCRECT = &H400
Public Const DT_CENTER = &H1
Public Const DT_EXPANDTABS = &H40
Public Const DT_EXTERNALLEADING = &H200
Public Const DT_LEFT = &H0
Public Const DT_NOCLIP = &H100
Public Const DT_NOPREFIX = &H800
Public Const DT_RIGHT = &H2
Public Const DT_SINGLELINE = &H20
Public Const DT_TABSTOP = &H80
Public Const DT_TOP = &H0
Public Const DT_VCENTER = &H4
Public Const DT_WORDBREAK = &H10
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Type SIZE
cx As Long
cy As Long
End Type
Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
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
Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As SIZE) As Long
放在form:
Option Explicit
Const LongStr = "Determines the width and height of the rectangle. If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text."
Private Sub Command1_Click()
Dim r As RECT, s As SIZE
r.Left = 50: r.Top = 20
DrawText Me.hDC, "Draw Text Test", 14, r, DT_CALCRECT
Line (r.Left, r.Top)-(r.Right, r.Bottom), RGB(255, 255, 0), BF
DrawText Me.hDC, "Draw Text Test", 14, r, 0
r.Left = 50: r.Top = 50: r.Right = 300
DrawText Me.hDC, LongStr, Len(LongStr), r, DT_CALCRECT Or DT_WORDBREAK
Line (r.Left, r.Top)-(r.Right, r.Bottom), RGB(255, 255, 0), BF
DrawText Me.hDC, LongStr, Len(LongStr), r, DT_WORDBREAK
End Sub