Function Reference


_WinAPI_GetTabbedTextExtent

Computes the width and height of a character string which may contain one or more tab characters

#include <WinAPIGdi.au3>
_WinAPI_GetTabbedTextExtent ( $hDC, $sText [, $aTab = 0 [, $iStart = 0 [, $iEnd = -1]]] )

Parameters

$hDC A handle to the device context.
$sText A character string.
$aTab [optional] The array containing the tab-stop positions, in device units. The tab stops must be sorted in increasing
order; the smallest x-value should be the first item in the array. Also, it can be an integer value that is
one tab-stop position. In this case, the tab stops are separated by the distance specified by this value.
If this parameter is 0 (Default), tabs are expanded to eight times the average character width.
$iStart [optional] The index of array element that contains the first tab-stop position.
$iEnd [optional] The index of array element that contains the last tab-stop position.

Return Value

Success: $tagSIZE structure that contains the dimensions of the string in logical units.
Failure: Sets the @error flag to non-zero.

Remarks

This function uses the currently selected font to compute the dimensions of the string.

Because some devices do not place characters in regular cell arrays (that is, they kern the characters), the
sum of the extents of the characters in a string may not be equal to the extent of the string.

The current clipping region does not affect the returned result.

See Also

Search GetTabbedTextExtent in MSDN Library.

Example

#include <GUIConstantsEx.au3>
#include <SendMessage.au3>
#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIHObj.au3>
#include <WindowsConstants.au3>

Local Const $sTxt = 'String' & @TAB & 'with' & @TAB & 'tab' & @TAB & 'characters'

; Create GUI
Local $hForm = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), 0, 0, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU))
GUICtrlCreateLabel($sTxt, 0, 0)
GUICtrlSetFont(-1, 26, 400, 0, 'Comic Sans MS')

; Compute dimensions of a string and resize the window
Local $iSize = _GetTabbedStringSize(-1, $sTxt)
If IsArray($iSize) Then
    GUICtrlSetPos(-1, 0, 0, $iSize[0], $iSize[1])
    Local $aPos = WinGetPos($hForm)
    If Not @error Then
        WinMove($hForm, '', (@DesktopWidth - ($aPos[2] + $iSize[0])) / 2, (@DesktopHeight - ($aPos[3] + $iSize[1])) / 2, $aPos[2] + $iSize[0], $aPos[3] + $iSize[1])
    EndIf
EndIf

; Show GUI
GUISetState(@SW_SHOW)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _GetTabbedStringSize($hWnd, $sText)
    Local $tTM = DllStructCreate($tagTEXTMETRIC)

    If Not IsHWnd($hWnd) Then
        $hWnd = GUICtrlGetHandle($hWnd)
        If Not $hWnd Then Return 0
    EndIf

    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $hFont = _SendMessage($hWnd, $WM_GETFONT)
    Local $hSv = _WinAPI_SelectObject($hDC, $hFont)
    Local $iTab = 0
    Local $aRet = DllCall('gdi32.dll', 'int', 'GetTextMetricsW', 'handle', $hDC, 'struct*', $tTM)
    If Not @error And $aRet[0] Then
        $iTab = 8 * DllStructGetData($tTM, 'tmAveCharWidth')
    EndIf
    Local $iSize = 0
    Local $tSIZE = _WinAPI_GetTabbedTextExtent($hDC, $sText, $iTab)
    If Not @error Then
        Dim $iSize[2]
        For $i = 0 To 1
            $iSize[$i] = DllStructGetData($tSIZE, $i + 1)
        Next
    EndIf
    _WinAPI_SelectObject($hDC, $hSv)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    Return $iSize
EndFunc   ;==>_GetTabbedStringSize