[green]' Modular Constants:[/green]
[blue]Private Const[/blue] FontSize% = 10
[blue]Private Const[/blue] LogPixelsX! = 116
[blue]Private Const[/blue] FontName$ = "Arial"[green]
'
' Modular Types:[/green]
[blue]Public Type[/blue] TextSize
cx [blue]As Long[/blue]
cy [blue]As Long[/blue]
[blue]End Type[/blue]
[blue]Private Type[/blue] LogFont
lfHeight [blue]As Long[/blue]
lfWidth [blue]As Long[/blue]
lfEscapement [blue]As Long[/blue]
lfOrientation [blue]As Long[/blue]
lfWeight [blue]As Long[/blue]
lfItalic [blue]As Byte[/blue]
lfUnderline [blue]As Byte[/blue]
lfStrikeOut [blue]As Byte[/blue]
lfCharSet [blue]As Byte[/blue]
lfOutPrecision [blue]As Byte[/blue]
lfClipPrecision [blue]As Byte[/blue]
lfQuality [blue]As Byte[/blue]
lfPitchAndFamily [blue]As Byte[/blue]
lfFaceName [blue]As String[/blue] * 32
[blue]End Type[/blue][green]
'
' Modular API Functions:[/green]
[blue]Private Declare Function[/blue] CreateDC& [blue]Lib[/blue] "gdi32.dll" [blue]Alias[/blue] "CreateDCA" _
([blue]ByVal[/blue] lpDriverName$, [blue]ByVal[/blue] lpDeviceName$, [blue]ByVal[/blue] lpOutput$, lpInitData&)
[blue]Private Declare Function[/blue] CreateCompatibleBitmap& [blue]Lib[/blue] "gdi32.dll" _
([blue]ByVal[/blue] hdc&, [blue]ByVal[/blue] nWidth&, [blue]ByVal[/blue] nHeight&)
[blue]Private Declare Function[/blue] CreateFontIndirect& [blue]Lib[/blue] "gdi32.dll" [blue]Alias[/blue] "CreateFontIndirectA" _
(lpLogFont [blue]As LogFont[/blue])
[blue]Private Declare Function[/blue] SelectObject& [blue]Lib[/blue] "gdi32.dll" _
([blue]ByVal[/blue] hdc&, [blue]ByVal[/blue] hObject&)
[blue]Private Declare Function[/blue] DeleteObject& [blue]Lib[/blue] "gdi32.dll" _
([blue]ByVal[/blue] hObject&)
[blue]Private Declare Function[/blue] GetTextExtentPoint32& [blue]Lib[/blue] "gdi32.dll" [blue]Alias[/blue] "GetTextExtentPoint32A" _
([blue]ByVal[/blue] hdc&, [blue]ByVal[/blue] lpsz$, [blue]ByVal[/blue] cbString&, lpSize [blue]As TextSize[/blue])
[blue]Private Declare Function[/blue] GetDC& [blue]Lib[/blue] "user32.dll" _
([blue]ByVal[/blue] hwnd&)
[blue]Private Declare Function[/blue] GetDeviceCaps& [blue]Lib[/blue] "gdi32.dll" _
([blue]ByVal[/blue] hdc&, [blue]ByVal[/blue] nIndex&)
[blue]Private Declare Function[/blue] DeleteDC& [blue]Lib[/blue] "gdi32.dll" _
([blue]ByVal[/blue] hdc&)
[blue]Option Explicit[/blue][green]
' •TextWidth Function•
' Purpose: Measure the size of txtString (in pixels).
' Arguments:
' txtString = String to be analyzed.[/green]
[blue]Public Function[/blue] TextWidth(txtString$) [blue]As[/blue] TextSize
[blue]Dim[/blue] tempDC&, tempBMP&, f&
[blue]Dim[/blue] lf [blue]As[/blue] LogFont[green]
' Create a device context and a bitmap that can be used to store a _
' temporary font object. Assign the bitmap to the device context.[/green]
tempDC = CreateDC("DISPLAY", vbNullString, vbNullString, [blue]ByVal[/blue] 0)
tempBMP = CreateCompatibleBitmap(tempDC, 1, 1)[green]
' Set up the LogFont structure and create the font.[/green]
lf.lfFaceName = FontName & Chr$(0)
lf.lfHeight = -FontSize * GetDeviceCaps(GetDC(0), LogPixelsX) / 72
f = CreateFontIndirect(lf)[green]
' Assign the font to the device context.[/green]
DeleteObject SelectObject(tempDC, f)[green]
' Measure the text, and return it as TextWidth.[/green]
GetTextExtentPoint32 tempDC, txtString, Len(txtString), TextWidth[green]
' Delete objects.[/green]
DeleteObject f
DeleteObject tempBMP
DeleteDC tempDC
[blue]End Function[/blue]