Multifunction text output function

xiaoxiao2021-03-05  25

This is a multi-function text output function I have written ourselves, providing a PRINT statement to fit the functionality of positioning, fonts, etc., can also provide automatic wrap, font rotation, invalid text and other functions.

Public Enum DrawTextAlign DT_LEFT = & H0 & DT_CENTER = & H1 & DT_RIGHT = & H2 & DT_TOP = & H0 & DT_VCENTER = & H4 & DT_BOTTOM = & H8 & End EnumPublic Enum DrawTextOption DT_EXTERNALLEADING = & H200 & DT_EXPANDTABS = & H40 & DT_EDITCONTROL = & H2000 & DT_PATH_ELLIPSIS = & H4000 & DT_END_ELLIPSIS = & H8000 & DT_MODIFYSTRING = & H10000 DT_RTLREADING = & H20000 DT_WORD_ELLIPSIS = & H40000End Enum

Public Type RECT Left As Long Top As Long Right As Long Bottom As LongEnd TypePrivate Enum DrawTextFlag DT_WORDBREAK = & H10 & DT_NOCLIP = & H100 & DT_CALCRECT = & H400 & DT_SINGLELINE = & H20 & End EnumPrivate Enum BackMode TRANSPARENT = 1 OPAQUE = 2End EnumPrivate Type LOGFONT lfHeight As Long lfWidth As Long lfEscapement As Long lfOrientation As Long lfWeight As Long lfItalic As Byte lfUnderline As Byte lfStrikeOut As Byte lfCharSet As Byte lfOutPrecision As Byte lfClipPrecision As Byte lfQuality As Byte lfPitchAndFamily As Byte lfFaceName (0 To 31) As ByteEnd TypePrivate Type Size cx As Long cy As LongEnd Type

Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As LongPrivate Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As LongPrivate Declare Function GetTextExtentPoint32 Lib "gdi32" Alias ​​"GetTextExtentPoint32A" (ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As Size) As LongPrivate Declare Function CreateFontIndirect Lib "gdi32" Alias ​​"CreateFontIndirectA" (lpLogFont As LOGFONT) As LongPrivate 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 LongPrivate 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 LongPrivate Declare Function SetBkMode Lib "gdi32" (ByVal hDC As Long, ByVal nBkMode As Long) As LongPrivate Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As LongPrivate Declare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As LongPrivate Declare Function lstrlen Lib "kernel32" Alias ​​"lstrlenA" (ByVal lpString As String) As LongPrivate Declare Sub CopyMemory Lib "kernel32" Alias ​​"RTLmoveMemory" (PDEST AS Any, PSource As Any, Byval Dwlength As Long)

Public Function TextPrint (ByVal dhDC As Long, ByVal Text As String, x As Long, y As Long, Optional ByVal w As Long, Optional ByVal h As Long, _ Optional TextColor As Long = -1, Optional DrawAlign As DrawTextAlign, Optional FontSize AS Long, Optional FontBold As long, Optional NewFont AS String, OPTIONAL DRAWOPT AS DRAWTEXTOTIONOPTION AS Rect 'Procedure Description:' In the target HDC Multi-function transparent mode output text 'This process can provide print The functionality that the statement needs to be fitted with positioning, fonts, etc., also provides font rotation, invalid text function 'When the option is not enabled, this process output text is about 20% of the PRINT statement is 20%' parameter description: 'must parameter' ----------- 'HDC Target DC' Text Output Text 'x, Y Start Location Left Corner Zone' ------------------- ---- 'Optional Parameters' -----------------------' Lineangle rotation angle, if not 0, you will not support multi-line, and The text is not cropped. The returned rectangle is only 0 normal rectangle 'TextColor 0 to specify the text color when the positive value is specified, and the original text color is used in -2, and the text is described as an invalid text output' FONTSIZE. The fontbold font is high and bold, the standard Song 9 word height is 12 'drawalign text alignment to the DrawTextalign constant' W, h Set the text rectangular width and high, W = 0 is a single line output 'When W> 0, ie For the automatic wrap text, pay attention, start this function, the output speed will drop 6-10 times 'newfont specified the font name, for empty, use the "Song"' DrawopT using the Flag output from the DrawTextOption constant Dim hFont As Long, hOldFont As Long Dim Font As LOGFONT, TextRect As RECT, hBrush As Long, tColor As Long Dim szText As Size, BkM As Long, LineOP As Long, UseDraw As Boolean If LineAngle <> 0 Or FontSize <> 0 Or FontBold <> -1 or len (newfont)> 0 dam = 134 .lfescapement = lineangle * 10 if Fontsize <> 0 Then if fontsize> 0 Then .lfheight =

-FontSize Else .lfHeight = FontSize End If Else .lfHeight = -12 End If .lfWidth = 0 If FontBold <> -1 Then If FontBold = 0 Then .lfWeight = 400 Else .lfWeight = 700 End If End If If NewFont <> vbNullString Then CopyMemory .lfFaceName (0), ByVal NewFont & vbNullChar, lstrlen (NewFont & vbNullChar) Else CopyMemory .lfFaceName (0), ByVal "Arial" & vbNullChar, lstrlen ( "Arial" & vbNullChar) End If End With hFont = CreateFontIndirect (Font) HOLDFONT = SELECTOBJECT (DHDC, HFONT) End ifness (DHDC, HFONT) End ifness (DHDC, HFONT) End if If TextColor <> -1 Theni TextColor <-1 TColor = VBWHIX W> 0 THEN W = W - 1 IF H> 0 THEN H = H - 1 End IF Else Tcolor = TextColor End If tColor = SetTextColor (dhDC, tColor) End If BkM = SetBkMode (dhDC, TRANSPARENT) GetTextExtentPoint32 dhDC, Text, lstrlen (Text), szText If LineAngle = 0 And w> 0 Then If DrawOpt <> 0 Then UseDraw = True LineOP = Drawopt end if if w 0 and h <= sztext.cy the lineop = lineop or dt_singleline sztext.cy = h else lineop =

LineOP Or DT_WORDBREAK TextRect.Right = w DrawText dhDC, Text & vbNullChar, -1, TextRect, LineOP Or DT_CALCRECT If h = 0 Or h> = szText.cy Then szText.cy = TextRect.Bottom LineOP = LineOP Or DT_NOCLIP Else szText. CY = h end if end if else if (lineop and dt_expandtabs) <> 0 Then Sztext.cx = w lineop = lineop or dt_singeline if h = 0 or h> = sztext.cy thrip = lineop or dt_noclip end if End = Case DrawAlign And (DT_CENTER Or DT_RIGHT) Case DT_LEFT TextRect.Left = x Case DT_CENTER TextRect.Left = x - szText.cx / 2 Case DT_RIGHT TextRect.Left = x - szText.cx End Select Select Case DrawAlign And (DT_VCENTER Or DT_BOTTOM) Case dt_top textRect.top = y case dt_vcenter textRect.top = y - sztext .cy / 2 Case DT_BOTTOM TextRect.Top = y - szText.cy End Select TextRect.Bottom = TextRect.Top szText.cy TextRect.Right = TextRect.Left szText.cx If UseDraw = False Then If TextColor <-1 Then TextOut dhDC, TextRect.Left 1, TextRect.Top 1, Text, lstrlen (Text) SetTextColor dhDC, & H808080 End If TextOut dhDC, TextRect.Left, TextRect.Top, Text, lstrlen (Text) Else If TextColor <

转载请注明原文地址:https://www.9cbs.com/read-35366.html

New Post(0)