Jump to content

_WINAPI_GetTextExtent32


martin
 Share

Recommended Posts

I do not see how I should use _WINAPI_GetTextExtent32, as this example shows.

#AutoIt3Wrapper_Add_Constants=n
#include <winapi.au3>


$gui1 = GUICreate("")
GUISetState()
GUISetFont(12)
GUICtrlCreateLabel("abcd",10,10)
 $strWH = _WinAPI_GetTextExtentPoint32(_WinAPI_GetDC($gui1),"abcd")
 ConsoleWrite("text width = " & dllstructgetdata($strWH,"Y") & @CRLF)

 GUISetFont(60)
 GUICtrlCreateLabel("abcd",10,70)
 $strWH = _WinAPI_GetTextExtentPoint32(_WinAPI_GetDC($gui1),"abcd")
 ConsoleWrite("text width = " & dllstructgetdata($strWH,"Y") & @CRLF)

sleep(9000)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

martin,

Take a look at this UDF I wrote to do get the size of strings so you can get the correct dimensions of labels etc.

#include-once

; #INDEX# ============================================================================================================
; Title .........: _StringSize
; AutoIt Version : v3.2.12.1 or higher
; Language ......: English
; Description ...: Returns size of rectangle required to display string - width can be chosen
; Remarks .......:
; Note ..........:
; Author(s) .....: Melba23
; ====================================================================================================================

;#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

; #INCLUDES# =========================================================================================================
#include <SendMessage.au3>
#include <WinAPI.au3>

; #CURRENT# ==========================================================================================================
; _StringSize: Returns size of rectangle required to display string - width can be chosen
; ====================================================================================================================

; #INTERNAL_USE_ONLY#=================================================================================================
; _StringSize_Error: Returns from error condition after DC and GUI clear up
; ====================================================================================================================

; #FUNCTION# =========================================================================================================
; Name...........: _StringSize
; Description ...: Returns size of rectangle required to display string - maximum permitted width can be chosen
; Syntax ........: _StringSize($sText[, $iSize[, $iWeight[, $iAttrib[, $sName[, $iWidth]]]]])
; Parameters ....: $sText   - String to display
;                  $iSize   - [optional] Font size in points - default AutoIt GUI default
;                  $iWeight - [optional] Font weight (400 = normal) - default AutoIt GUI default
;                  $iAttrib - [optional] Font attribute (0-Normal, 2-Italic, 4-Underline, 8 Strike - default AutoIt
;                  $sName   - [optional] Font name - default AutoIt GUI default
;                  $iWidth  - [optional] Width of rectangle - default is unwrapped width of string
; Requirement(s) : v3.2.12.1 or higher
; Return values .: Success - Returns array with details of rectangle required for text:
;                  |$array[0] = String formatted with @CRLF at required wrap points
;                  |$array[1] = Height of single line in selected font
;                  |$array[2] = Width of rectangle required to hold formatted string
;                  |$array[3] = Height of rectangle required to hold formatted string
;                  Failure - Returns 0 and sets @error:
;                  |1 - Incorrect parameter type (@extended = parameter index)
;                  |2 - Failure to create GUI to test label size
;                  |3 - DLL call error - extended set to indicate which
;                  |4 - Font too large for chosen width - longest word will not fit
; Author ........: Melba23
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
;=====================================================================================================================
Func _StringSize($sText, $iSize = Default, $iWeight = Default, $iAttrib = Default, $sName = Default, $iWidth = 0)

    Local $avSize_Info[4], $iLine_Width = 0, $iLast_Word, $iWrap_Count
    Local $hLabel_Handle, $hFont, $hDC, $oFont, $tSize

    ; Check parameters are correct type
    If Not IsString($sText) Then Return SetError(1, 1, 0)
    If Not IsNumber($iSize) And $iSize <> Default   Then Return SetError(1, 2, 0)
    If Not IsInt($iWeight)  And $iWeight <> Default Then Return SetError(1, 3, 0)
    If Not IsInt($iAttrib)  And $iAttrib <> Default Then Return SetError(1, 4, 0)
    If Not IsString($sName) And $sName <> Default   Then Return SetError(1, 5, 0)
    If Not IsNumber($iWidth) Then Return SetError(1, 6, 0)

    ; Create GUI to contain test labels, set to passed font parameters
    Local $hGUI = GUICreate("", 1200, 500, @DesktopWidth + 10, @DesktopHeight + 10)
        If $hGUI = 0 Then Return SetError(2, 0, 0)
        GUISetFont($iSize, $iWeight, $iAttrib, $sName)

    ; Store unwrapped text
    $avSize_Info[0] = $sText

    ; Ensure EoL is @CRLF and break text into lines
    If StringInStr($sText, @CRLF) = 0 Then StringRegExpReplace($sText, "[\x0a|\x0d]", @CRLF)
    Local $asLines = StringSplit($sText, @CRLF, 1)

    ; Draw label with unwrapped lines to check on max width
    Local $hText_Label = GUICtrlCreateLabel($sText, 10, 10)
    Local $aiPos = ControlGetPos($hGUI, "", $hText_Label)

    ;GUISetState(@SW_HIDE)

    GUICtrlDelete($hText_Label)

    ; Store line height for this font size after removing label padding (always 8)
    $avSize_Info[1] = ($aiPos[3] - 8)/ $asLines[0]
    ; Store width and height of this label
    $avSize_Info[2] = $aiPos[2]
    $avSize_Info[3] = $aiPos[3] - 4 ; Reduce margin

    ; Check if wrapping is required
    If $aiPos[2] > $iWidth And $iWidth > 0 Then

        ; Set returned text element to null
        $avSize_Info[0] = ""

        ; Set width element to max allowed
        $avSize_Info[2] = $iWidth

        ; Set line count to zero
        Local $iLine_Count = 0

        ; Take each line in turn
        For $j = 1 To $asLines[0]

            ; Size this line unwrapped
            $hText_Label = GUICtrlCreateLabel($asLines[$j], 10, 10)
            $aiPos = ControlGetPos($hGUI, "", $hText_Label)
            GUICtrlDelete($hText_Label)

            ; Check wrap status
            If $aiPos[2] < $iWidth Then
                ; No wrap needed so count line and store
                $iLine_Count += 1
                $avSize_Info[0] &= $asLines[$j] & @CRLF
            Else
                ; Wrap needed so need to count wrapped lines

                ; Create label to hold line as it grows
                $hText_Label = GUICtrlCreateLabel("", 0, 0)
                ; Initialise Point32 method
                $hLabel_Handle = ControlGetHandle($hGui, "", $hText_Label)
                $hDC = _WinAPI_GetDC($hLabel_Handle)
                If @error Then _StringSize_Error(3, 1, $hLabel_Handle, 0, $hGUI)
                $hFont = _SendMessage($hLabel_Handle, 0x0031) ; $WM_GETFONT
                If @error Then _StringSize_Error(3, 2, $hLabel_Handle, $hDC, $hGUI)
                $oFont = _WinAPI_SelectObject($hDC, $hFont)
                If @error Then _StringSize_Error(3, 3, $hLabel_Handle, $hDC, $hGUI)
                If $oFont = 0 Then _StringSize_Error(3, 4, $hLabel_Handle, $hDC, $hGUI)

                ; Zero counter
                $iWrap_Count = 0

                While 1

                    ; Set line width to 0
                    $iLine_Width = 0
                    ; Initialise pointer for end of word
                    $iLast_Word = 0

                    For $i = 1 To StringLen($asLines[$j])

                        ; Is this just past a word ending?
                        If StringMid($asLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1
                        ; Increase line by one character
                        Local $sTest_Line = StringMid($asLines[$j], 1, $i)
                        ; Place line in label
                        GUICtrlSetData($hText_Label, $sTest_Line)
                        ; Get line length
                        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sTest_Line)
                        If @error Then _StringSize_Error(3, 5, $hLabel_Handle, $hDC, $hGUI)
                        $iLine_Width = DllStructGetData($tSize, "X")

                        ; If too long exit the loop
                        If $iLine_Width >= $iWidth - Int($iSize / 2) Then ExitLoop
                    Next

                    ; End of the line of text?
                    If $i > StringLen($asLines[$j]) Then
                        ; Yes, so add final line to count
                        $iWrap_Count += 1
                        ; Store line
                        $avSize_Info[0] &= $sTest_Line & @CRLF
                        ExitLoop
                    Else
                        ; No, but add line just completed to count
                        $iWrap_Count += 1
                        ; Check at least 1 word completed or return error
                        If $iLast_Word = 0 Then
                            _StringSize_Error(4, 0, $hLabel_Handle, $hDC, $hGUI)
                        EndIf
                        ; Store line up to end of last word
                        $avSize_Info[0] &= StringLeft($sTest_Line, $iLast_Word) & @CRLF
                        ; Strip string to point reached
                        $asLines[$j] = StringTrimLeft($asLines[$j], $iLast_Word)
                        ; Trim leading whitespace
                        $asLines[$j] = StringStripWS($asLines[$j], 1)
                        ; Repeat with remaining characters in line
                    EndIf

                WEnd

                ; Add the number of wrapped lines to the count
                $iLine_Count += $iWrap_Count

                ; Clean up
                _WinAPI_ReleaseDC($hLabel_Handle, $hDC)
                GUICtrlDelete($hText_Label)

            EndIf

        Next

        ; Convert lines to pixels and add reduced margin
        $avSize_Info[3] = ($iLine_Count * $avSize_Info[1]) + 4

    EndIf

    ; Clean up
    GUIDelete($hGUI)

    ; Return array
    Return $avSize_Info

EndFunc ; => _StringSize

; #INTERNAL_USE_ONLY#============================================================================================================
; Name...........: _StringSize_Error
; Description ...: Returns from error condition after DC and GUI clear up
; Syntax ........: _StringSize_Error($iError, $iExtended, $hLabel_Handle, $hDC, $hGUI)
; Parameters ....: $iError  - required error value to return
;                  $iExtended - required extended value to return
;                  $hLabel_Handle, $hDC, $hGUI - variables as set in _StringSize function
; Author ........: Melba23
; Modified.......:
; Remarks .......: This function is used internally by _StringSize
; ===============================================================================================================================
Func _StringSize_Error($iError, $iExtended, $hLabel_Handle, $hDC, $hGUI)

    ; Release DC if created
    DllCall("User32.dll", "int", "ReleaseDC", "hwnd", $hLabel_Handle, "hwnd", $hDC)
    ; Delete GUI
    GUIDelete($hGUI)
    ; Return with extended set
    Return SetError($iError, $iExtended, 0)

EndFunc ; => _StringSize_Error

I know it is a bit more complicated that you wanted, but the important bit is where the _WINAPI_GetTextExtent32 line is.

Please ask if anything is unclear. :evil:

M23

Edit - code altered after PsaltyDS's discovery! ;)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

martin,

Take a look at this UDF I wrote to do get the size of strings so you can get the correct dimensions of labels etc.

I know it is a bit more complicated that you wanted, but the important bit is where the _WINAPI_GetTextExtent32 line is.

Please ask if anything is unclear. :evil:

M23

Please ask if

I had already tried changing martin's to use the control handle vice window handle (no change), but yours didn't work for me either. I combined both like this:
#include <winapi.au3>
#include <SendMessage.au3>


$gui1 = GUICreate("Test", 300, 200)
$ctrlLabel1 = GUICtrlCreateLabel("abcde", 10, 10, 280, 30)
$hLabel1 = ControlGetHandle($gui1, "", $ctrlLabel1)
GUICtrlSetFont(-1, 14)

$ctrlLabel2 = GUICtrlCreateLabel("abcde", 10, 70, 280, 100)
$hLabel2 = ControlGetHandle($gui1, "", $ctrlLabel2)
GUICtrlSetFont(-1, 60)

GUISetState()

$hDC = _WinAPI_GetDC($hLabel1)
$strWH = _WinAPI_GetTextExtentPoint32($hDC, "abcde")
ConsoleWrite("WinAPI text width = " & DllStructGetData($strWH, "Y") & @CRLF)
_WinAPI_ReleaseDC($hLabel1, $hDC)

$aRET = _StringSize("abcde", 14)
If Not @error Then ConsoleWrite("_StringSize[0] = " & $aRET[0] & "; [1] = " & $aRET[1] & "; [2] = " & $aRET[2] & "; [3] = " & $aRET[3] & @LF)

$hDC = _WinAPI_GetDC($hLabel2)
$strWH = _WinAPI_GetTextExtentPoint32($hDC, "abcde")
ConsoleWrite("WinAPI text width = " & DllStructGetData($strWH, "Y") & @CRLF)
_WinAPI_ReleaseDC($hLabel2, $hDC)

$aRET = _StringSize("abcde", 60)
If Not @error Then ConsoleWrite("_StringSize[0] = " & $aRET[0] & "; [1] = " & $aRET[1] & "; [2] = " & $aRET[2] & "; [3] = " & $aRET[3] & @LF)

Sleep(9000)


; #FUNCTION# =========================================================================================================
; Name...........: _StringSize
; Description ...: Returns size of rectangle required to display string - maximum permitted width can be chosen
; Syntax ........: _StringSize($sText[, $iSize[, $iWeight[, $iAttrib[, $sName[, $iWidth]]]]])
; Parameters ....: $sText   - String to display
;                  $iSize   - [optional] Font size in points - default AutoIt GUI default
;                  $iWeight - [optional] Font weight (400 = normal) - default AutoIt GUI default
;                  $iAttrib - [optional] Font attribute (0-Normal, 2-Italic, 4-Underline, 8 Strike - default AutoIt
;                  $sName   - [optional] Font name - default AutoIt GUI default
;                  $iWidth  - [optional] Width of rectangle - default is unwrapped width of string
; Requirement(s) : v3.2.12.1 or higher
; Return values .: Success - Returns array with details of rectangle required for text:
;                  |$array[0] = String formatted with @CRLF at required wrap points
;                  |$array[1] = Height of single line in selected font
;                  |$array[2] = Width of rectangle required to hold formatted string
;                  |$array[3] = Height of rectangle required to hold formatted string
;                  Failure - Returns 0 and sets @error:
;                  |1 - Incorrect parameter type (@extended = parameter index)
;                  |2 - Failure to create GUI to test label size
;                  |3 - DLL call error - extended set to indicate which
;                  |4 - Font too large for chosen width - longest word will not fit
; Author ........: Melba23
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
;=====================================================================================================================
Func _StringSize($sText, $iSize = Default, $iWeight = Default, $iAttrib = Default, $sName = Default, $iWidth = 0)

    Local $avSize_Info[4], $iLine_Width = 0, $iLast_Word, $iWrap_Count
    Local $hLabel_Handle, $hFont, $hDC, $oFont, $tSize

    ; Check parameters are correct type
    If Not IsString($sText) Then Return SetError(1, 1, 0)
    If Not IsNumber($iSize) And $iSize <> Default   Then Return SetError(1, 2, 0)
    If Not IsInt($iWeight)  And $iWeight <> Default Then Return SetError(1, 3, 0)
    If Not IsInt($iAttrib)  And $iAttrib <> Default Then Return SetError(1, 4, 0)
    If Not IsString($sName) And $sName <> Default   Then Return SetError(1, 5, 0)
    If Not IsNumber($iWidth) Then Return SetError(1, 6, 0)

    ; Create GUI to contain test labels, set to passed font parameters
    Local $hGUI = GUICreate("", 1200, 500, 10, 10)
        If $hGUI = 0 Then Return SetError(2, 0, 0)
        GUISetFont($iSize, $iWeight, $iAttrib, $sName)

    ; Store unwrapped text
    $avSize_Info[0] = $sText

    ; Ensure EoL is @CRLF and break text into lines
    If StringInStr($sText, @CRLF) = 0 Then StringRegExpReplace($sText, "[\x0a|\x0d]", @CRLF)
    Local $asLines = StringSplit($sText, @CRLF, 1)

    ; Draw label with unwrapped lines to check on max width
    Local $hText_Label = GUICtrlCreateLabel($sText, 10, 10)
    Local $aiPos = ControlGetPos($hGUI, "", $hText_Label)
    GUICtrlDelete($hText_Label)

    ; Store line height for this font size after removing label padding (always 8)
    $avSize_Info[1] = ($aiPos[3] - 8)/ $asLines[0]
    ; Store width and height of this label
    $avSize_Info[2] = $aiPos[2]
    $avSize_Info[3] = $aiPos[3] - 4 ; Reduce margin

    ; Check if wrapping is required
    If $aiPos[2] > $iWidth And $iWidth > 0 Then

        ; Set returned text element to null
        $avSize_Info[0] = ""

        ; Set width element to max allowed
        $avSize_Info[2] = $iWidth

        ; Set line count to zero
        Local $iLine_Count = 0

        ; Take each line in turn
        For $j = 1 To $asLines[0]

            ; Size this line unwrapped
            $hText_Label = GUICtrlCreateLabel($asLines[$j], 10, 10)
            $aiPos = ControlGetPos($hGUI, "", $hText_Label)
            GUICtrlDelete($hText_Label)

            ; Check wrap status
            If $aiPos[2] < $iWidth Then
                ; No wrap needed so count line and store
                $iLine_Count += 1
                $avSize_Info[0] &= $asLines[$j] & @CRLF
            Else
                ; Wrap needed so need to count wrapped lines

                ; Create label to hold line as it grows
                $hText_Label = GUICtrlCreateLabel("", 0, 0)
                ; Initialise Point32 method
                $hLabel_Handle = ControlGetHandle($hGui, "", $hText_Label)
                $hDC = _WinAPI_GetDC($hLabel_Handle)
                If @error Then _StringSize_Error(3, 1, $hLabel_Handle, 0, $hGUI)
                $hFont = _SendMessage($hLabel_Handle, 0x0031) ; $WM_GETFONT
                If @error Then _StringSize_Error(3, 2, $hLabel_Handle, $hDC, $hGUI)
                $oFont = _WinAPI_SelectObject($hDC, $hFont)
                If @error Then _StringSize_Error(3, 3, $hLabel_Handle, $hDC, $hGUI)
                If $oFont = 0 Then _StringSize_Error(3, 4, $hLabel_Handle, $hDC, $hGUI)

                ; Zero counter
                $iWrap_Count = 0

                While 1

                    ; Set line width to 0
                    $iLine_Width = 0
                    ; Initialise pointer for end of word
                    $iLast_Word = 0

                    For $i = 1 To StringLen($asLines[$j])

                        ; Is this just past a word ending?
                        If StringMid($asLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1
                        ; Increase line by one character
                        Local $sTest_Line = StringMid($asLines[$j], 1, $i)
                        ; Place line in label
                        GUICtrlSetData($hText_Label, $sTest_Line)
                        ; Get line length
                        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sTest_Line)
                        If @error Then _StringSize_Error(3, 5, $hLabel_Handle, $hDC, $hGUI)
                        $iLine_Width = DllStructGetData($tSize, "X")

                        ; If too long exit the loop
                        If $iLine_Width >= $iWidth - Int($iSize / 2) Then ExitLoop
                    Next

                    ; End of the line of text?
                    If $i > StringLen($asLines[$j]) Then
                        ; Yes, so add final line to count
                        $iWrap_Count += 1
                        ; Store line
                        $avSize_Info[0] &= $sTest_Line & @CRLF
                        ExitLoop
                    Else
                        ; No, but add line just completed to count
                        $iWrap_Count += 1
                        ; Check at least 1 word completed or return error
                        If $iLast_Word = 0 Then
                            _StringSize_Error(4, 0, $hLabel_Handle, $hDC, $hGUI)
                        EndIf
                        ; Store line up to end of last word
                        $avSize_Info[0] &= StringLeft($sTest_Line, $iLast_Word) & @CRLF
                        ; Strip string to point reached
                        $asLines[$j] = StringTrimLeft($asLines[$j], $iLast_Word)
                        ; Trim leading whitespace
                        $asLines[$j] = StringStripWS($asLines[$j], 1)
                        ; Repeat with remaining characters in line
                    EndIf

                WEnd

                ; Add the number of wrapped lines to the count
                $iLine_Count += $iWrap_Count

                ; Clean up
                _WinAPI_ReleaseDC($hLabel_Handle, $hDC)
                GUICtrlDelete($hText_Label)

            EndIf

        Next

        ; Convert lines to pixels and add reduced margin
        $avSize_Info[3] = ($iLine_Count * $avSize_Info[1]) + 4

    EndIf

    ; Clean up
    GUIDelete($hGUI)

    ; Return array
    Return $avSize_Info

EndFunc ; => _StringSize

; #INTERNAL_USE_ONLY#============================================================================================================
; Name...........: _StringSize_Error
; Description ...: Returns from error condition after DC and GUI clear up
; Syntax ........: _StringSize_Error($iError, $iExtended, $hLabel_Handle, $hDC, $hGUI)
; Parameters ....: $iError  - required error value to return
;                  $iExtended - required extended value to return
;                  $hLabel_Handle, $hDC, $hGUI - variables as set in _StringSize function
; Author ........: Melba23
; Modified.......:
; Remarks .......: This function is used internally by _StringSize
; ===============================================================================================================================
Func _StringSize_Error($iError, $iExtended, $hLabel_Handle, $hDC, $hGUI)

    ; Release DC if created
    DllCall("User32.dll", "int", "ReleaseDC", "hwnd", $hLabel_Handle, "hwnd", $hDC)
    ; Delete GUI
    GUIDelete($hGUI)
    ; Return with extended set
    Return SetError($iError, $iExtended, 0)

EndFunc ; => _StringSize_Error

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks for th ereplies guys, I'll take a look later.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

PsaltyDS,

Thanks for finding that bug! ;) I so rarely use the function for single line strings I had not noticed that it did not treat them correctly.

It should work correctly now:

#include-once

; #INDEX# ============================================================================================================
; Title .........: _StringSize
; AutoIt Version : v3.2.12.1 or higher
; Language ......: English
; Description ...: Returns size of rectangle required to display string - width can be chosen
; Remarks .......:
; Note ..........:
; Author(s) .....: Melba23
; ====================================================================================================================

;#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

; #INCLUDES# =========================================================================================================
#include <SendMessage.au3>
#include <WinAPI.au3>

; #CURRENT# ==========================================================================================================
; _StringSize: Returns size of rectangle required to display string - width can be chosen
; ====================================================================================================================

; #INTERNAL_USE_ONLY#=================================================================================================
; _StringSize_Error: Returns from error condition after DC and GUI clear up
; ====================================================================================================================

; #FUNCTION# =========================================================================================================
; Name...........: _StringSize
; Description ...: Returns size of rectangle required to display string - maximum permitted width can be chosen
; Syntax ........: _StringSize($sText[, $iSize[, $iWeight[, $iAttrib[, $sName[, $iWidth]]]]])
; Parameters ....: $sText   - String to display
;                  $iSize   - [optional] Font size in points - default AutoIt GUI default
;                  $iWeight - [optional] Font weight (400 = normal) - default AutoIt GUI default
;                  $iAttrib - [optional] Font attribute (0-Normal, 2-Italic, 4-Underline, 8 Strike - default AutoIt
;                  $sName   - [optional] Font name - default AutoIt GUI default
;                  $iWidth  - [optional] Width of rectangle - default is unwrapped width of string
; Requirement(s) : v3.2.12.1 or higher
; Return values .: Success - Returns array with details of rectangle required for text:
;                  |$array[0] = String formatted with @CRLF at required wrap points
;                  |$array[1] = Height of single line in selected font
;                  |$array[2] = Width of rectangle required to hold formatted string
;                  |$array[3] = Height of rectangle required to hold formatted string
;                  Failure - Returns 0 and sets @error:
;                  |1 - Incorrect parameter type (@extended = parameter index)
;                  |2 - Failure to create GUI to test label size
;                  |3 - DLL call error - extended set to indicate which
;                  |4 - Font too large for chosen width - longest word will not fit
; Author ........: Melba23
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
;=====================================================================================================================
Func _StringSize($sText, $iSize = Default, $iWeight = Default, $iAttrib = Default, $sName = Default, $iWidth = 0)

    Local $avSize_Info[4], $iLine_Width = 0, $iLast_Word, $iWrap_Count
    Local $hLabel_Handle, $hFont, $hDC, $oFont, $tSize

    ; Check parameters are correct type
    If Not IsString($sText) Then Return SetError(1, 1, 0)
    If Not IsNumber($iSize) And $iSize <> Default   Then Return SetError(1, 2, 0)
    If Not IsInt($iWeight)  And $iWeight <> Default Then Return SetError(1, 3, 0)
    If Not IsInt($iAttrib)  And $iAttrib <> Default Then Return SetError(1, 4, 0)
    If Not IsString($sName) And $sName <> Default   Then Return SetError(1, 5, 0)
    If Not IsNumber($iWidth) Then Return SetError(1, 6, 0)

    ; Create GUI to contain test labels, set to passed font parameters
    Local $hGUI = GUICreate("", 1200, 500, @DesktopWidth + 10, @DesktopHeight + 10)
        If $hGUI = 0 Then Return SetError(2, 0, 0)
        GUISetFont($iSize, $iWeight, $iAttrib, $sName)

    ; Store unwrapped text
    $avSize_Info[0] = $sText

    ; Ensure EoL is @CRLF and break text into lines
    If StringInStr($sText, @CRLF) = 0 Then StringRegExpReplace($sText, "[\x0a|\x0d]", @CRLF)
    Local $asLines = StringSplit($sText, @CRLF, 1)

    ; Draw label with unwrapped lines to check on max width
    Local $hText_Label = GUICtrlCreateLabel($sText, 10, 10)
    Local $aiPos = ControlGetPos($hGUI, "", $hText_Label)

    GUISetState(@SW_HIDE)

    GUICtrlDelete($hText_Label)

    ; Store line height for this font size after removing label padding (always 8)
    $avSize_Info[1] = ($aiPos[3] - 8)/ $asLines[0]
    ; Store width and height of this label
    $avSize_Info[2] = $aiPos[2]
    $avSize_Info[3] = $aiPos[3] - 4 ; Reduce margin

    ; Check if wrapping is required
    If $aiPos[2] > $iWidth And $iWidth > 0 Then

        ConsoleWrite("Hit" & @CRLF)


        ; Set returned text element to null
        $avSize_Info[0] = ""

        ; Set width element to max allowed
        $avSize_Info[2] = $iWidth

        ; Set line count to zero
        Local $iLine_Count = 0

        ; Take each line in turn
        For $j = 1 To $asLines[0]

            ; Size this line unwrapped
            $hText_Label = GUICtrlCreateLabel($asLines[$j], 10, 10)
            $aiPos = ControlGetPos($hGUI, "", $hText_Label)
            GUICtrlDelete($hText_Label)

            ; Check wrap status
            If $aiPos[2] < $iWidth Then
                ; No wrap needed so count line and store
                $iLine_Count += 1
                $avSize_Info[0] &= $asLines[$j] & @CRLF
            Else
                ; Wrap needed so need to count wrapped lines

                ; Create label to hold line as it grows
                $hText_Label = GUICtrlCreateLabel("", 0, 0)
                ; Initialise Point32 method
                $hLabel_Handle = ControlGetHandle($hGui, "", $hText_Label)
                $hDC = _WinAPI_GetDC($hLabel_Handle)
                If @error Then _StringSize_Error(3, 1, $hLabel_Handle, 0, $hGUI)
                $hFont = _SendMessage($hLabel_Handle, 0x0031) ; $WM_GETFONT
                If @error Then _StringSize_Error(3, 2, $hLabel_Handle, $hDC, $hGUI)
                $oFont = _WinAPI_SelectObject($hDC, $hFont)
                If @error Then _StringSize_Error(3, 3, $hLabel_Handle, $hDC, $hGUI)
                If $oFont = 0 Then _StringSize_Error(3, 4, $hLabel_Handle, $hDC, $hGUI)

                ; Zero counter
                $iWrap_Count = 0

                While 1

                    ; Set line width to 0
                    $iLine_Width = 0
                    ; Initialise pointer for end of word
                    $iLast_Word = 0

                    For $i = 1 To StringLen($asLines[$j])

                        ; Is this just past a word ending?
                        If StringMid($asLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1
                        ; Increase line by one character
                        Local $sTest_Line = StringMid($asLines[$j], 1, $i)
                        ; Place line in label
                        GUICtrlSetData($hText_Label, $sTest_Line)
                        ; Get line length
                        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sTest_Line)
                        If @error Then _StringSize_Error(3, 5, $hLabel_Handle, $hDC, $hGUI)
                        $iLine_Width = DllStructGetData($tSize, "X")

                        ; If too long exit the loop
                        If $iLine_Width >= $iWidth - Int($iSize / 2) Then ExitLoop
                    Next

                    ; End of the line of text?
                    If $i > StringLen($asLines[$j]) Then
                        ; Yes, so add final line to count
                        $iWrap_Count += 1
                        ; Store line
                        $avSize_Info[0] &= $sTest_Line & @CRLF
                        ExitLoop
                    Else
                        ; No, but add line just completed to count
                        $iWrap_Count += 1
                        ; Check at least 1 word completed or return error
                        If $iLast_Word = 0 Then
                            _StringSize_Error(4, 0, $hLabel_Handle, $hDC, $hGUI)
                        EndIf
                        ; Store line up to end of last word
                        $avSize_Info[0] &= StringLeft($sTest_Line, $iLast_Word) & @CRLF
                        ; Strip string to point reached
                        $asLines[$j] = StringTrimLeft($asLines[$j], $iLast_Word)
                        ; Trim leading whitespace
                        $asLines[$j] = StringStripWS($asLines[$j], 1)
                        ; Repeat with remaining characters in line
                    EndIf

                WEnd

                ; Add the number of wrapped lines to the count
                $iLine_Count += $iWrap_Count

                ; Clean up
                _WinAPI_ReleaseDC($hLabel_Handle, $hDC)
                GUICtrlDelete($hText_Label)

            EndIf

        Next

        ; Convert lines to pixels and add reduced margin
        $avSize_Info[3] = ($iLine_Count * $avSize_Info[1]) + 4

    EndIf

    ; Clean up
    GUIDelete($hGUI)

    ; Return array
    Return $avSize_Info

EndFunc ; => _StringSize

; #INTERNAL_USE_ONLY#============================================================================================================
; Name...........: _StringSize_Error
; Description ...: Returns from error condition after DC and GUI clear up
; Syntax ........: _StringSize_Error($iError, $iExtended, $hLabel_Handle, $hDC, $hGUI)
; Parameters ....: $iError  - required error value to return
;                  $iExtended - required extended value to return
;                  $hLabel_Handle, $hDC, $hGUI - variables as set in _StringSize function
; Author ........: Melba23
; Modified.......:
; Remarks .......: This function is used internally by _StringSize
; ===============================================================================================================================
Func _StringSize_Error($iError, $iExtended, $hLabel_Handle, $hDC, $hGUI)

    ; Release DC if created
    DllCall("User32.dll", "int", "ReleaseDC", "hwnd", $hLabel_Handle, "hwnd", $hDC)
    ; Delete GUI
    GUIDelete($hGUI)
    ; Return with extended set
    Return SetError($iError, $iExtended, 0)

EndFunc ; => _StringSize_Error

And so martin's script becomes:

#include "StringSize.au3"

$sString = "abcd"

$aResult = _StringSize($sString, 12)
ConsoleWrite("String width = " & $aResult[2] & @CRLF)

$aResult = _StringSize($sString, 24)
ConsoleWrite("String width = " & $aResult[2] & @CRLF)

which gives me 43 and 75 as the widths at the 2 sizes.

M23

Edit: Speeling!

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Ace, I never new about this, its exactly what I need to determine the width of my gui

I used StringLen() * the average pixel width of the font, which isnt so Ace

Glad I seen this thread

Cheers all

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

So to summarize, martin should have get handle of the used font and select it into the DC before calling _WinAPI_GetTextExtentPoint32().

Is that correct?

Yes, it is.

And to make martin's GUI work with the existing _WinAPI_GetTextExtentPoint32():
#include <winapi.au3>
#include <SendMessage.au3>


$gui1 = GUICreate("Test", 300, 200)
$ctrlLabel1 = GUICtrlCreateLabel("abcde", 10, 10, 280, 30)
$hLabel1 = ControlGetHandle($gui1, "", $ctrlLabel1)
GUICtrlSetFont(-1, 14)

$ctrlLabel2 = GUICtrlCreateLabel("abcde", 10, 70, 280, 100)
$hLabel2 = ControlGetHandle($gui1, "", $ctrlLabel2)
GUICtrlSetFont(-1, 60)

GUISetState()

$hDC = _WinAPI_GetDC($hLabel1)
$hFont = _SendMessage($hLabel1, 0x0031) ; $WM_GETFONT
$oFont = _WinAPI_SelectObject($hDC, $hFont)
$strWH = _WinAPI_GetTextExtentPoint32($hDC, "abcde")
ConsoleWrite("WinAPI text width = " & DllStructGetData($strWH, "Y") & @CRLF)
_WinAPI_ReleaseDC($hLabel1, $hDC)

$hDC = _WinAPI_GetDC($hLabel2)
$hFont = _SendMessage($hLabel2, 0x0031) ; $WM_GETFONT
$oFont = _WinAPI_SelectObject($hDC, $hFont)
$strWH = _WinAPI_GetTextExtentPoint32($hDC, "abcde")
ConsoleWrite("WinAPI text width = " & DllStructGetData($strWH, "Y") & @CRLF)
_WinAPI_ReleaseDC($hLabel2, $hDC)

Sleep(9000)

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

And to make martin's GUI work with the existing _WinAPI_GetTextExtentPoint32():

#include <winapi.au3>
#include <SendMessage.au3>


$gui1 = GUICreate("Test", 300, 200)
$ctrlLabel1 = GUICtrlCreateLabel("abcde", 10, 10, 280, 30)
$hLabel1 = ControlGetHandle($gui1, "", $ctrlLabel1)
GUICtrlSetFont(-1, 14)

$ctrlLabel2 = GUICtrlCreateLabel("abcde", 10, 70, 280, 100)
$hLabel2 = ControlGetHandle($gui1, "", $ctrlLabel2)
GUICtrlSetFont(-1, 60)

GUISetState()

$hDC = _WinAPI_GetDC($hLabel1)
$hFont = _SendMessage($hLabel1, 0x0031) ; $WM_GETFONT
$oFont = _WinAPI_SelectObject($hDC, $hFont)
$strWH = _WinAPI_GetTextExtentPoint32($hDC, "abcde")
ConsoleWrite("WinAPI text width = " & DllStructGetData($strWH, "Y") & @CRLF)
_WinAPI_ReleaseDC($hLabel1, $hDC)

$hDC = _WinAPI_GetDC($hLabel2)
$hFont = _SendMessage($hLabel2, 0x0031) ; $WM_GETFONT
$oFont = _WinAPI_SelectObject($hDC, $hFont)
$strWH = _WinAPI_GetTextExtentPoint32($hDC, "abcde")
ConsoleWrite("WinAPI text width = " & DllStructGetData($strWH, "Y") & @CRLF)
_WinAPI_ReleaseDC($hLabel2, $hDC)

Sleep(9000)

;)

OK, thanks. I think I've got it now.

You can't use $WM_GETFONT for the window or for a graphic control it seems, so I translate your examples into this which I was trying to do after seeing this thread..

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Global $tRECT, $hFont, $hOldFont, $hDC

HotKeySet("{ESC}", "_Exit")

$g1 = GUICreate("")
GUISetState()

$x = GUICtrlCreateGraphic(0, 0, 400, 400)

GUICtrlSetGraphic(-1, $GUI_GR_PENSIZE, 5)
GUICtrlSetBkColor(-1, 0x00ff00) ; Green
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x4f00ff)
GUICtrlSetGraphic(-1, $GUI_GR_RECT, 50, 50, 180, 180)
GUICtrlSetGraphic(-1, $GUI_GR_REFRESH) ; show what is drawn

Sleep(900)

GUICtrlSetGraphic(-1, $GUI_GR_PENSIZE, 1)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x000000) ; size before color
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 250, 150)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 250, 350)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 380, 350)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 350, 350)

GUICtrlSetGraphic(-1, $GUI_GR_RECT, 350, 250, 2, 5)

GUICtrlSetGraphic(-1, $GUI_GR_REFRESH) ; show what is drawn



GUIRegisterMsg($WM_PAINT, "MyPaint")
MyPaint()

While GUIGetMsg() <> -3
WEnd
_Exit()

Func _Exit()
    Exit
EndFunc ;==>_Exit

Func MyPaint()
    $tRECT = DllStructCreate($tagRect)
    DllStructSetData($tRECT, "Left", 5)
    DllStructSetData($tRECT, "Top", 5)


    $hDC = _WinAPI_GetDC(GUICtrlGetHandle($x));the graphic control
    $hFont = _WinAPI_CreateFont(40, 0, 0, 0, 400, False, False, False, $DEFAULT_CHARSET, _
    $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')
    ; $hFont = _SendMessage(GUICtrlGetHandle($x), 0x0031) ; $WM_GETFONT <-- doesn't work for a graphic or a gui window (or is the graphic really just the gui dc?)
    $hOldFont = _WinAPI_SelectObject($hDC, $hFont)
    $strWH = _WinAPI_GetTextExtentPoint32($hDC, "Hello world!")
    DllStructSetData($tRECT, "Right", DllStructGetData($strWH, "X") + 5)
    DllStructSetData($tRECT, "Bottom", DllStructGetData($strWH, "Y") + 5)

    _WinAPI_SetTextColor($hDC, 0x0000FF)
    _WinAPI_SetBkColor($hDC, 0x000000)
    ; comment next line to get black background instead of transparent one
    _WinAPI_SetBkMode($hDC, $TRANSPARENT)
    _WinAPI_DrawText($hDC, "Hello world!", $tRECT, $DT_CENTER)

    _WinAPI_SelectObject($hDC, $hOldFont)
    _WinAPI_DeleteObject($hFont)
    _WinAPI_ReleaseDC($g1, $hDC)
    ; _WinAPI_InvalidateRect(0, 0)
EndFunc ;==>MyPaint

Thanks for sorting this out for me PsaltyDS and Melba23, I couldn't see what was needed from the msdn info on GetTextExtentPoint32W.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Were you surprised? At least just a bit?

Am I able to surprise you after all...?

Yes trancexx , but no one is surprised any more that you are able to surprise ;)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

To get standard font you can use GetStockObject API instead of

$hFont = _WinAPI_CreateFont(40, 0, 0, 0, 400, False, False, False, $DEFAULT_CHARSET, _
    $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')

http://msdn.microsoft.com/en-us/library/dd144925(VS.85).aspx

It can return:

ANSI_FIXED_FONT

ANSI_VAR_FONT

DEVICE_DEFAULT_FONT

DEFAULT_GUI_FONT

OEM_FIXED_FONT

SYSTEM_FONT

SYSTEM_FIXED_FONT

Edited by Zedna
Link to comment
Share on other sites

Yes trancexx , but no one is surprised any more that you are able to surprise ;)

Maybe.

_WinAPI_CreateFont() is too raw. Size is in logical units, for example.

I would go (and am) with different version:

Func _CreateFont($iSize = 8.5, $iWeight = 400, $iAttribute = 0, $sFontName = "", $hWnd = 0)

    Local $iItalic = BitAND($iAttribute, 2)
    Local $iUnderline = BitAND($iAttribute, 4)
    Local $iStrikeout = BitAND($iAttribute, 8)

    Local $iQuality
    If BitAND($iAttribute, 16) Then
        $iQuality = 1 ; DRAFT_QUALITY
    ElseIf BitAND($iAttribute, 32) Then
        $iQuality = 2 ; PROOF_QUALITY
    ElseIf BitAND($iAttribute, 64) Then
        $iQuality = 3 ; NONANTIALIASED_QUALITY
    ElseIf BitAND($iAttribute, 128) Then
        $iQuality = 4 ; ANTIALIASED_QUALITY
    ElseIf BitAND($iAttribute, 256) Then
        $iQuality = 5 ; CLEARTYPE_QUALITY
    ElseIf BitAND($iAttribute, 512) Then
        $iQuality = 6 ; CLEARTYPE_COMPAT_QUALITY
    EndIf

    Local $aCall = DllCall("user32.dll", "ptr", "GetDC", "hwnd", $hWnd)

    If @error Or Not $aCall[0] Then
        Return SetError(1, 0, 0)
    EndIf

    Local $hDC = $aCall[0]

    $aCall = DllCall("gdi32.dll", "int", "GetDeviceCaps", _
            "ptr", $hDC, _
            "int", 90) ; LOGPIXELSY

    If @error Or Not $aCall[0] Then
        Return SetError(2, 0, 0)
    EndIf

    Local $iDCaps = $aCall[0]

    DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $hWnd, "ptr", $hDC)

    $aCall = DllCall("gdi32.dll", "ptr", "CreateFontW", _
            "int", -$iSize * $iDCaps / 72, _
            "int", 0, _
            "int", 0, _
            "int", 0, _
            "int", $iWeight, _
            "dword", $iItalic, _
            "dword", $iUnderline, _
            "dword", $iStrikeout, _
            "dword", 0, _
            "dword", 0, _
            "dword", 0, _
            "dword", $iQuality, _
            "dword", 0, _
            "wstr", $sFontName)

    If @error Or Not $aCall[0] Then
        Return SetError(3, 0, 0)
    EndIf

    Local $hFont = $aCall[0]

    Return $hFont

EndFunc   ;==>_CreateFont

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Maybe.

_WinAPI_CreateFont() is too raw. Size is in logical units, for example.

I would go (and am) with different version:

Wow. Nice and... surprising!

;)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...