by Jon Mooty AKA YoungBuck (5 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Thu 1st November 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Returns the height and width in pixels of a string based on the current font characteristics of a device context (DC) using the
API Declarations
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As Size) As Long
Private Type Size
cx As Long
cy As Long
End Type
Dim szText As Size
Dim iStrLen As Long
Dim strText As String
Dim lRet As Long
strText = "HELLO WORLD"
iStrLen = Len(strText) ' retrieve the number of characters in the string
lRet = GetTextExtentPoint32(Picture1.hdc, strText, iStrLen, szText) ' retrieve the height and width of the string
If Not lRet = 0 Then
'if function call was successful then display the dimensions of the string based on the current font of _
the picture, to test this try changing the font characteristics of the Picture box and run this function _
again
Label1.Caption = "The string " & strText & " is " & _
szText.cx & " pixels wide and " & _
szText.cy & " pixels high!"
Else
' if the function call failed alert the user via the label
Label1.Caption = "API CALL DID NOT FUNCTION PROPERLY!!"
End If
End Sub
No comments have been posted about Returns the height and width in pixels of a string based on the current font characteristics of a d. Why not be the first to post a comment about Returns the height and width in pixels of a string based on the current font characteristics of a d.