Jump to content

Problem with _WinAPI_GetTextMetrics


guiguy
 Share

Recommended Posts

I am having a problem using _WinAPI_GetTextMetrics with AutoIt version 3.3.8.1

The function seems to give the same values for text with different font sizes.

In particular elements "tmHeight" and "tmMaxCharWidth".

The following script will show the problem.

It should first be run with font size 8.5 enabled and font size 20 commented out,

then again with font size 20 enabled.

Values "tmHeight" and "tmMaxCharWidth" are the same for both fonts!

Why is that? (Or what am I doing wrong?)

#cs
# Example showing difficulty with _WinAPI_GetTextMetrics
#ce
Const  $GUI_Font_Size = 8.5     ;Run script first using this
;Const  $GUI_Font_Size = 20     ;Run script again using this
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
Const  $GUI_Width  = 600
Const  $GUI_Height = 200
Const  $GUI_Left   = -1 ; Centered
Const  $GUI_Top = 100
Const  $GUI_Style  = $WS_BORDER
Const  $GUI_Parent = -1
Const  $Lbl_Txt  = "Any text string - gjpqy at font size " & _
    String($GUI_Font_Size)
Const  $AppTitle = "Get font info"
Const  $MSGBOX_STOPSIGN = 0x10
;Screen pixel scaling in twips per pixel (twip = 1/1440 inch = 1/20 point)
Global $Dsply_X_Scale = _WinAPI_TwipsPerPixelX()
Global $Dsply_Y_Scale = _WinAPI_TwipsPerPixelY()
#cs
#================
# Main Routine
#================
#ce
; Create the GUI window
Local $GUI_HNDL = GUICreate($AppTitle, $GUI_Width, $GUI_Height, _
    $GUI_Left, $GUI_Top, $GUI_Style, $GUI_Parent)
CheckForError($GUI_HNDL, "Error creating the GUI window")

; Set the font size
Local $Rtn = GUISetFont($GUI_Font_Size, 400, 0, "", $GUI_HNDL, 5)
CheckForError($Rtn, "Error setting the font attributes")

; Add the label
Local $Lbl_CID = GUICtrlCreateLabel($Lbl_Txt, -1, _
    -1, ($GUI_Width - 50), ($GUI_Height - 50), $SS_CENTER)
CheckForError($Lbl_CID, "Error creating the alert label")

; Display the GUI
$Rtn = GUISetState(@SW_SHOW,$GUI_HNDL)
CheckForError($Rtn, "Error showing the GUI")

; Get the font info - allocate the info struct
Local $Metrics = DllStructCreate($tagTEXTMETRIC)
CheckSysError(@error, "Error [" & String(@error) & _
    "] creating struct $tagTEXTMETRIC")

;Get the decive context
Local $hDC = _WinAPI_GetDC($GUI_HNDL)
CheckForError($hDC, "Error getting device context")

; Get the metrics
$Metrics = _WinAPI_GetTextMetrics($hDC)
CheckSysError(@error, "Error getting text metric structure")

; Free the device context
$Rtn = _WinAPI_ReleaseDC($GUI_HNDL, $hDC)
CheckForError($Rtn, "Error releasing device context")

; Pull out the desired information
Local $FontHeight = DllStructGetData($Metrics, "tmHeight")
CheckSysError(@error, "Struct error [" & String(@error) & _
    "] getting tmHeight")

Local $FontMaxCharWidth = DllStructGetData($Metrics, "tmMaxCharWidth")
CheckSysError(@error, "Struct error [" & String(@error) & _
    "] getting tmMaxCharWidth")
  
Local $tmAscent = DllStructGetData($Metrics, "tmAscent")
CheckSysError(@error, "Struct error [" & String(@error) & _
    "] getting tmAscent")
  
Local $tmDescent = DllStructGetData($Metrics, "tmDescent")
CheckSysError(@error, "Struct error [" & String(@error) & _
    "] getting tmDescent")

$Metrics = 0    ;Free the struct memory

MsgBox(0, $AppTitle, _
    "tmHeight      = " & String($FontHeight) & @CRLF & _
    "tmMaxCharWidth = " & String($FontMaxCharWidth) & @CRLF & _
    "tmAscent      = " & String($tmAscent) & @CRLF & _
    "tmDescent    = " & String($tmDescent) & @CRLF & _
    "Vert res      = " & String(@DesktopHeight) & @CRLF & _
    "Horz res      = " & String(@DesktopWidth) & @CRLF & _
    "Dsply Y twips per pixel = " & String($Dsply_Y_Scale) & @CRLF & _
    "Dsply X twips per pixel = " & String($Dsply_X_Scale))
#cs
#--------------
# Func Abort
#--------------
#ce
Func Abort($Msg = "Aborting...", $Rtn = -1)
        MsgBox(Bitor(0, $MSGBOX_STOPSIGN), $AppTitle, _
            $Msg & ", aborting...")
        Exit $Rtn
EndFunc
#cs
#----------------------------------------------------------
# Func CheckForError
#----------------------------------------------------------
# Tests the provided return code for error, and aborts with
# a message if there is one.
# Error => code is zero
#----------------------------------------------------------
#ce
Func CheckForError($code, $msg)
    if $code = 0 then
        Abort($msg)
    endif
EndFunc  ;CheckForError
#cs
#----------------------------------------------------------
# Func CheckSysError
#----------------------------------------------------------
# Tests the provided return code for system error, and
# aborts with a message if there is one.
# Error => code is non-zero
#----------------------------------------------------------
#ce
Func CheckSysError($code, $msg)
    if $code <> 0 then
        Abort($msg)
    endif
EndFunc   ; CheckSysError
Edited by guiguy

-- guiguy --

Link to comment
Share on other sites

  • 2 weeks later...

Try this:

#cs
# Example showing difficulty with _WinAPI_GetTextMetrics
#ce
;~ Const  $GUI_Font_Size = 8.5     ;Run script first using this
Const  $GUI_Font_Size = 20     ;Run script again using this
#include <FontConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Const  $GUI_Width  = 600
Const  $GUI_Height = 200
Const  $GUI_Left   = -1 ; Centered
Const  $GUI_Top = 100
Const  $GUI_Style  = $WS_BORDER
Const  $GUI_Parent = -1
Const  $Lbl_Txt  = "Any text string - gjpqy at font size " & String($GUI_Font_Size)
Const  $AppTitle = "Get font info"
Const  $MSGBOX_STOPSIGN = 0x10
;Screen pixel scaling in twips per pixel (twip = 1/1440 inch = 1/20 point)
Global $Dsply_X_Scale = _WinAPI_TwipsPerPixelX()
Global $Dsply_Y_Scale = _WinAPI_TwipsPerPixelY()
#cs
#================
# Main Routine
#================
#ce
; Create the GUI window
Local $GUI_HNDL = GUICreate($AppTitle, $GUI_Width, $GUI_Height, _
    $GUI_Left, $GUI_Top, $GUI_Style, $GUI_Parent)
CheckForError($GUI_HNDL, "Error creating the GUI window")

; Set the font size
Local $Rtn = GUISetFont($GUI_Font_Size, 400, 0, "", $GUI_HNDL, 5)
CheckForError($Rtn, "Error setting the font attributes")

; Add the label
Local $Lbl_CID = GUICtrlCreateLabel($Lbl_Txt, -1, _
    -1, ($GUI_Width - 50), ($GUI_Height - 50), $SS_CENTER)
CheckForError($Lbl_CID, "Error creating the alert label")

; Display the GUI
$Rtn = GUISetState(@SW_SHOW,$GUI_HNDL)
CheckForError($Rtn, "Error showing the GUI")

; Get the font info - allocate the info struct
Local $Metrics = DllStructCreate($tagTEXTMETRIC)
CheckSysError(@error, "Error [" & String(@error) & "] creating struct $tagTEXTMETRIC")

;Get the decive context
Local $hDC = _WinAPI_GetDC($GUI_HNDL)
CheckForError($hDC, "Error getting device context")

$hFont = _WinAPI_CreateFont($GUI_Font_Size, 0, 0, 0, 400, False, False, False, $DEFAULT_CHARSET, _
        $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')
$hOldFont = _WinAPI_SelectObject($hDC, $hFont)

; Get the metrics
$Metrics = _WinAPI_GetTextMetrics($hDC)
CheckSysError(@error, "Error getting text metric structure")

; Free the device context
$Rtn = _WinAPI_ReleaseDC($GUI_HNDL, $hDC)
CheckForError($Rtn, "Error releasing device context")

; Pull out the desired information
Local $FontHeight = DllStructGetData($Metrics, "tmHeight")
CheckSysError(@error, "Struct error [" & String(@error) & _
    "] getting tmHeight")

Local $FontMaxCharWidth = DllStructGetData($Metrics, "tmMaxCharWidth")
CheckSysError(@error, "Struct error [" & String(@error) & _
    "] getting tmMaxCharWidth")

Local $tmAscent = DllStructGetData($Metrics, "tmAscent")
CheckSysError(@error, "Struct error [" & String(@error) & _
    "] getting tmAscent")

Local $tmDescent = DllStructGetData($Metrics, "tmDescent")
CheckSysError(@error, "Struct error [" & String(@error) & _
    "] getting tmDescent")

$Metrics = 0    ;Free the struct memory

MsgBox(0, $AppTitle, _
    "tmHeight      = " & String($FontHeight) & @CRLF & _
    "tmMaxCharWidth = " & String($FontMaxCharWidth) & @CRLF & _
    "tmAscent      = " & String($tmAscent) & @CRLF & _
    "tmDescent    = " & String($tmDescent) & @CRLF & _
    "Vert res      = " & String(@DesktopHeight) & @CRLF & _
    "Horz res      = " & String(@DesktopWidth) & @CRLF & _
    "Dsply Y twips per pixel = " & String($Dsply_Y_Scale) & @CRLF & _
    "Dsply X twips per pixel = " & String($Dsply_X_Scale))

    _WinAPI_SelectObject($hDC, $hOldFont)
    _WinAPI_DeleteObject($hFont)
     _WinAPI_ReleaseDC($GUI_HNDL, $hDC)
    Exit
#cs
#--------------
# Func Abort
#--------------
#ce
Func Abort($Msg = "Aborting...", $Rtn = -1)
        MsgBox(Bitor(0, $MSGBOX_STOPSIGN), $AppTitle, _
            $Msg & ", aborting...")
        Exit $Rtn
EndFunc
#cs
#----------------------------------------------------------
# Func CheckForError
#----------------------------------------------------------
# Tests the provided return code for error, and aborts with
# a message if there is one.
# Error => code is zero
#----------------------------------------------------------
#ce
Func CheckForError($code, $msg)
    if $code = 0 then
        Abort($msg)
    endif
EndFunc  ;CheckForError
#cs
#----------------------------------------------------------
# Func CheckSysError
#----------------------------------------------------------
# Tests the provided return code for system error, and
# aborts with a message if there is one.
# Error => code is non-zero
#----------------------------------------------------------
#ce
Func CheckSysError($code, $msg)
    if $code <> 0 then
        Abort($msg)
    endif
EndFunc   ; CheckSysError

You didn't set the font for HDC. That's the reason why it is always the same.

Do not be so impatient or did you paid for support?

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...