Jump to content

Splash Text


Recommended Posts

Hello, I'm working on an automation, which I'll use to record a tutorial video for something. I use this code to show messages:

SplashTextOn("Splash", "", 330, 40, -1, -1, 1, "Verdana")
For $i = 5 To 0 Step -1
    ControlSetText("Splash", "", "Static1", "Automation starting in " & $i & " seconds.")
    Sleep(1000)
Next
SplashOff()

I am planning to use it a lot so I need to make it into a function but the problem is, that I need the window size to be a little bigger than my text (like tooltips). The messages are one line only so the only thing I need to modify is the width. So, I'm wondering if there is any way to append the width of the splash window depending on text length without having to manually set a "random" value and check if the size will feet the whole text.

P.S. The new forum system (design/layout and the autoit code function in posts) is awesome, good job :)

Link to comment
Share on other sites

  • Moderators

Asd123456,

First, welcome to the AutoIt forums.

I wrote this UDF a while ago to get the size of strings to fit into labels and messageboxes, but it will also do what you want. I have included a small example of how you might use it with the code you posted:

#include-once

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

;#####################  EXAMPLE  ###############################

$sText = "Automation starting in # seconds."
$aStringSize = _StringSize($sText, 12, Default, Default, "Verdana")

SplashTextOn("Splash", "", $aStringSize[2] + 15, 40, -1, -1, 1, "Verdana")  ; See end of post for explanation of + 15
For $i = 5 To 0 Step -1
    ControlSetText("Splash", "", "Static1", "Automation starting in " & $i & " seconds.")
    Sleep(1000)
Next
SplashOff()



;##############################################################

; #FUNCTION# =======================================================================================
;
; Name...........: _StringSize
; Description ...: Returns size of rectangle required to display string - width can be chosen
; Syntax ........: _StringSize($sText[, $iSize[, $iWeight[, $iAttrib[, $sName[, $iWidth]]]]])
; Parameters ....: $sText   - String to display
;                  $iSize   - Font size in points - default AutoIt GUI default
;                  $iWeight - Font weight (400 = normal) - default AutoIt GUI default
;                  $iAttrib - Font attribute (0-Normal, 2-Italic, 4-Underline, 8 Strike - default AutoIt
;                  $sName   - 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 - Failure of _WinAPI_SelectObject
;                  |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 $hWnd, $hFont, $hDC, $oFont, $tSize, $hGUI, $hText_Label, $sTest_Line
    Local $iLine_Count, $iLine_Width, $iWrap_Count, $iLast_Word
    Local $asLines[1], $avSize_Info[4], $aiPos[4]

    ; 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
    $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)
    $asLines = StringSplit($sText, @CRLF, 1)

    ; Draw label with unwrapped lines to check on max width
    $hText_Label = GUICtrlCreateLabel($sText, 10, 10)
    $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
        $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
                $hWnd = ControlGetHandle($hGui, "", $hText_Label)
                $hFont = _SendMessage($hWnd, $WM_GETFONT)
                $hDC = _WinAPI_GetDC($hWnd)
                $oFont = _WinAPI_SelectObject($hDC, $hFont)
                If $oFont = 0 Then Return SetError(3, 0, 0)

                ; 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
                        $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)
                        $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
                            GUIDelete($hGUI)
                            Return SetError(4, 0, 0)
                        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($hWnd, $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

To use it in your script, just delete the part between the ;############# lines and save the remainder in the same folder as your script (perhaps as StringSize.au3) Then add the line #include "StringSize.au3" to your script.

In case you are wondering why you need to add 15 to the result, the length returned by the UDF is exactly the length of the string (to within a pixel) and the SplashText box needs a bit of a margin - as I discovered when I was testing the code I posted! :)

M23

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

Thanks a lot! This is my outcome if anyone is interested.

#include <StringSize.au3>

...

_Message("Starting Automation")

...

Func _Message($m)   
    $aStringSize = _StringSize($m, 12, Default, Default, "Verdana")
    SplashTextOn("", $m, $aStringSize[2] + 15, 40, -1, -1, 1, "Verdana")
    Sleep(3000)
    SplashOff()
EndFunc
Edited by Asd123456
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...