Having trouble positioning text in gui
Posted: 14 Jun 2021, 00:58
I am not new to Autohotkey, I have built a few simple scripts, including one GUI, for use at work over the last five years, but this one's got me scratching my head.
Desired Outcome: A hotkey to display the current time and current date (or could be repurposed into a library function to display any text) in a screen overlay.
What is working: It does display the semi-transparent gui with text inside of it.
What is not working: Proper sizing of the final GUI. It should be just a bit larger than the two GUI Text controls stacked one above the other, but the text is starting much farther down and right of where I expect it to be.
Keep in mind I intend to add these functions into a library which could be used for displaying any number of rows of text in this overlay fashion, thus the relative complexity of the code. I know it can be done more simply without all the functions.
Desired Outcome: A hotkey to display the current time and current date (or could be repurposed into a library function to display any text) in a screen overlay.
What is working: It does display the semi-transparent gui with text inside of it.
What is not working: Proper sizing of the final GUI. It should be just a bit larger than the two GUI Text controls stacked one above the other, but the text is starting much farther down and right of where I expect it to be.
Keep in mind I intend to add these functions into a library which could be used for displaying any number of rows of text in this overlay fashion, thus the relative complexity of the code. I know it can be done more simply without all the functions.
Code: Select all
SetWorkingDir %A_ScriptDir%
#NoEnv
#SingleInstance force
#Persistent
OnExit, EOF ; register OnExit
DetectHiddenWindows, On
SetTitleMatchMode, 2
; display the current time and date when pressing Shift+RightCtrl
+RCtrl:: ; Right Windows key
; Setup overlay window
WindowID := OverlayWindow()
; Setup row to display current time
FormatTime, nowtime, , h:mm tt
ControlID := OverlayRow(72, nowTime)
TempSize := OverlaySize(ControlID)
; Set initial overlay window size
FinalW := TempSize[1]
FinalH := TempSize[2]
; Setup row to display current date
FormatTime, nowdate, , MMM dd, yyyy
ControlID := OverlayRow(48, nowDate)
TempSize := OverlaySize(ControlID)
; Adjust overlay window size if needed
If (FinalW < TempSize[1])
FinalW := TempSize[1]
FinalH := FinalH + TempSize[2]
; Show overlay window
OverlayShow(FinalW, FinalH, 666)
Return
Return
OverlayWindow() {
BGC = 333333
Gui, 1: +AlwaysOnTop +ToolWindow -Caption +E0x20 ; Click through GUI always on top.
Gui, 1: Color, %BGC%
Gui, 1: +LastFound
WinSet, Transparent, 160
OverlayWin := WinExist()
Return OverlayWin
}
OverlayRow(fontSize, textToShow) {
global
BGC = 333333
TextFont := "Trebuchet MS Bold"
TextSize := fontSize
; set some text colors to be chosen randomly, just for fun
FGColorsList := ["cAA8888", "c88AA88", "c8888AA", "cAAAA88", "cAA88AA", "c88AAAA"]
Random, randint, 1, % FGColorsList.MaxIndex()
FGC := FGColorsList[randint]
Gui, 1: Font, S%TextSize%, %TextFont%
Gui, 1: Add, Text, xp-2 yp-2 %FGC% BackgroundTrans, %textToShow%
GuiControlGet, thisCtrl, hwnd, %textToShow%
Return thisCtrl
}
OverlaySize(thisHwnd) {
global
TOSizeW := 0
TOSizeH := 0
; Set size of semi-transparent rectangle
GuiControlGet, TOSize, Pos, %thisHwnd%
; final gui should be 32 pixels wider and 8 pixels taller than both text controls
ThisWidth := TOSizeW + 32
ThisHeight := TOSizeH + 8
return [ThisWidth, ThisHeight]
}
; show the final gui with both text controls (set some reasonable default values for size)
OverlayShow(FinalWidth=450, FinalHeight=120, showTime=1000) {
global
Gui, 1: Show, w%FinalWidth% h%FinalHeight% NoActivate
Sleep %showTime%
Gui, 1: Destroy
}
EOF:
ExitApp