I would like to share with you a function that I use to splash text on the screen. The function SplashText uses an unnamed/unnumbered GUI window to show a text control. The goal was to create a simple but customizable function to replace SplashTextOn/SplashTextOff, especially given that those commands are no longer available in AutoHotkey v2 [link].
Here is the function:
Code: Select all
; ===============================================================================================================================
; SplashText(Text := "", Title := "", Options := "", Timeout := 0)
; Function: Creates a customizable GUI window with text in it. Use SplashText() to destroy the window. Calling the function
; with any text while a SplashText window is being displayed, destroys the previous window and creates a new one.
; The window is "always on top", meaning that it stays above all other normal windows. To change this, use
; WinSet, AlwaysOnTop, Off, ahk_id %GuiHwnd%. The window and the text in it can be customized through the
; Options parameter (see below). The current thread's default GUI window name or number is not changed.
; Parameters: Text - (Optional) The text to be displayed. If omitted, any existing SplashText window will be destroyed.
; Title - (Optional) The title of the GUI window. If omitted, the file name of the script will be used.
; Options - (Optional) An associative array with any of the following keys:
; Font - Font options separated by spaces (https://autohotkey.com/docs/commands/Gui.htm#Font)
; FontName - The name of the font (https://autohotkey.com/docs/misc/FontsStandard.htm)
; Text - Text options separated by spaces (https://autohotkey.com/docs/commands/Gui.htm#OtherOptions)
; BackColor - The background color of the window (https://autohotkey.com/docs/commands/Gui.htm#Color)
; Show - Gui, Show options separated by spaces (https://autohotkey.com/docs/commands/Gui.htm#Show)
; Note: If any of the keys are omitted, the value associated with that same key in the DefaultOptions static
; array will be used. If Options is omitted, the function generates a window similar to the
; window created with the SplashTextOn command.
; Timeout - (Optional) The time in seconds after which the window is automatically destroyed. If this parameter
; is omitted, the window will continue to be shown until it is destroyed using SplashText().
; Return values: The handle of the GUI window if one was created or a blank, otherwise.
; Global vars: None
; Depenencies: None
; Requirements: AHK v1.1.23+
; Tested with: AHK v1.1.36.02 (A32/U32/U64)
; Tested on: Win 10 Pro (x64)
; Written by: iPhilip
; Forum link: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=48681
; ===============================================================================================================================
SplashText(Text := "", Title := "", Options := "", Timeout := 0)
{
static GuiHwnd, DefaultOptions := {Font:"s11", FontName:"Segoe UI", Text:"Center", BackColor:"", Show:""}
if GuiHwnd
{
SetTimer, SplashText, Off
Gui, %GuiHwnd%:Destroy
GuiHwnd := ""
}
if Text
{
DefaultGui := A_DefaultGui ; Remember the current thread's default GUI window name or number.
Options := IsObject(Options) ? Options : {}
Gui, New, +AlwaysOnTop +Owner -SysMenu +HwndGuiHwnd, % Title
Gui, Font, % Options.Font ? Options.Font : DefaultOptions.Font
, % Options.FontName ? Options.FontName : DefaultOptions.FontName
Gui, Add, Text, % Options.Text ? Options.Text : DefaultOptions.Text, % Text
Gui, Color, % Options.BackColor ? Options.BackColor : DefaultOptions.BackColor
Gui, Show, % Options.Show ? Options.Show : DefaultOptions.Show
Gui, %DefaultGui%:Default ; Restore the current thread's default GUI window name or number.
if (Timeout > 0)
SetTimer, SplashText, % -Timeout * 1000
return GuiHwnd
}
}
Code: Select all
#NoEnv
;
; Simple example
;
Text := "Hello world!`nHow are you?"
MsgBox,
(
First, let's create a simple window using the SplashText function.
The window will automatically close after 3 seconds.
Click OK to continue.
)
SplashText(Text)
Sleep, 3000
SplashText()
;
; Comparison of SplashTextOn and SplashText
;
MsgBox,
(
Now, let's compare two windows: one generated using the SplashTextOn command and another one generated using the SplashText function.
Click OK to continue.
)
x := A_ScreenWidth // 2
y := A_ScreenHeight // 2 - 275
SplashTextOn, 140, 70, SplashTextOn, % Text
WinMove, SplashTextOn, , x - 152, y
SplashText(Text, "SplashText()", {Show:"x" (x + 6) " y" y})
MsgBox,
(
Note the two windows above.
The one on the left was generated using the SplashTextOn command.
The one on the right was generated using the SplashText function.
Notice the differences and similarities.
Click OK to continue.
)
SplashTextOff
SplashText()
;
; Countdown timer
;
Seconds := 5
Text := "Hello world!`nThis window will close`nin " Seconds " seconds."
MsgBox,
(
Finally, let's demonstrate the use of GuiControl to modify the text in the SplashText window.
Click OK to continue.
)
GuiHwnd := SplashText(Text)
SetTimer, Timer, 1000
WinWaitClose, ahk_id %GuiHwnd%
MsgBox, Done!
ExitApp
Timer:
if (Seconds > 1) {
Text := StrReplace(Text, Seconds, Seconds-1)
Text := --Seconds = 1 ? StrReplace(Text, "seconds", "second") : Text
GuiControl, %GuiHwnd%:, Static1, % Text
} else {
SetTimer, , Off
SplashText()
}
return
; ===============================================================================================================================
; SplashText(Text := "", Title := "", Options := "", Timeout := 0)
; Function: Creates a customizable GUI window with text in it. Use SplashText() to destroy the window. Calling the function
; with any text while a SplashText window is being displayed, destroys the previous window and creates a new one.
; The window is "always on top", meaning that it stays above all other normal windows. To change this, use
; WinSet, AlwaysOnTop, Off, ahk_id %GuiHwnd%. The window and the text in it can be customized through the
; Options parameter (see below). The current thread's default GUI window name or number is not changed.
; Parameters: Text - (Optional) The text to be displayed. If omitted, any existing SplashText window will be destroyed.
; Title - (Optional) The title of the GUI window. If omitted, the file name of the script will be used.
; Options - (Optional) An associative array with any of the following keys:
; Font - Font options separated by spaces (https://autohotkey.com/docs/commands/Gui.htm#Font)
; FontName - The name of the font (https://autohotkey.com/docs/misc/FontsStandard.htm)
; Text - Text options separated by spaces (https://autohotkey.com/docs/commands/Gui.htm#OtherOptions)
; BackColor - The background color of the window (https://autohotkey.com/docs/commands/Gui.htm#Color)
; Show - Gui, Show options separated by spaces (https://autohotkey.com/docs/commands/Gui.htm#Show)
; Note: If any of the keys are omitted, the value associated with that same key in the DefaultOptions static
; array will be used. If Options is omitted, the function generates a window similar to the
; window created with the SplashTextOn command.
; Timeout - (Optional) The time in seconds after which the window is automatically destroyed. If this parameter
; is omitted, the window will continue to be shown until it is destroyed using SplashText().
; Return values: The handle of the GUI window if one was created or a blank, otherwise.
; Global vars: None
; Depenencies: None
; Requirements: AHK v1.1.23+
; Tested with: AHK v1.1.36.02 (A32/U32/U64)
; Tested on: Win 10 Pro (x64)
; Written by: iPhilip
; Forum link: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=48681
; ===============================================================================================================================
SplashText(Text := "", Title := "", Options := "", Timeout := 0)
{
static GuiHwnd, DefaultOptions := {Font:"s11", FontName:"Segoe UI", Text:"Center", BackColor:"", Show:""}
if GuiHwnd
{
SetTimer, SplashText, Off
Gui, %GuiHwnd%:Destroy
GuiHwnd := ""
}
if Text
{
DefaultGui := A_DefaultGui ; Remember the current thread's default GUI window name or number.
Options := IsObject(Options) ? Options : {}
Gui, New, +AlwaysOnTop +Owner -SysMenu +HwndGuiHwnd, % Title
Gui, Font, % Options.Font ? Options.Font : DefaultOptions.Font
, % Options.FontName ? Options.FontName : DefaultOptions.FontName
Gui, Add, Text, % Options.Text ? Options.Text : DefaultOptions.Text, % Text
Gui, Color, % Options.BackColor ? Options.BackColor : DefaultOptions.BackColor
Gui, Show, % Options.Show ? Options.Show : DefaultOptions.Show
Gui, %DefaultGui%:Default ; Restore the current thread's default GUI window name or number.
if (Timeout > 0)
SetTimer, SplashText, % -Timeout * 1000
return GuiHwnd
}
}
Non-braking changes:
- Removed ByRef in the function parameters.
- Added the BackColor and Show keys to the DefaultOptions object.
- Added a Timeout parameter to automatically close the window after the specified period.
AHK v2 version
I hope you find it useful.
- iPhilip
EDIT: Fixed typos and added a line to disable/delete the timer when destroying the Gui.