Having trouble positioning text in gui Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
freiheit
Posts: 7
Joined: 16 Oct 2020, 23:37

Having trouble positioning text in gui

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.

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
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Having trouble positioning text in gui  Topic is solved

14 Jun 2021, 04:16

Code: Select all

   Gui, 1: Margin, 16, 4
might be usefull in this case. You don't need to calculate the Gui's dimensions then:

Code: Select all

#NoEnv
#SingleInstance force
#Persistent
OnExit, EOF   ; register OnExit
DetectHiddenWindows, On
SetTitleMatchMode, 2
SetWorkingDir %A_ScriptDir%


; 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)
	OverlayShow(666)
Return


Return

OverlayWindow(LRM := 16, TBM := 4) { ; LRM: left & right margin, TBM : top & bottom margin
	BGC = 333333
	Gui, 1: +AlwaysOnTop +ToolWindow -Caption +E0x20 ; Click through GUI always on top.
	Gui, 1: Color, %BGC%
	Gui, 1: Margin, %LRM%, %TBM%     ; <<<<<<<<<< added  
	Gui, 1: +LastFound
	WinSet, Transparent, 160
	OverlayWin := WinExist()
	Return OverlayWin
}

OverlayRow(fontSize, textToShow) {
	; global  <- no reason for global, afaics
	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, xm y+0 %FGC% BackgroundTrans, %textToShow%     ; <<<<<<<<<< changed
	GuiControlGet, thisCtrl, hwnd, %textToShow%
	Return thisCtrl
}

OverlaySize(thisHwnd) {     ; <<<<<<<<<<< not needed any more
	; global  <- no reason for global, afaics
	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(showTime=1000) {     ; <<<<<<<<<< changed
	; global  <- no reason for global, afaics
	Gui, 1: Show, NoActivate     ; <<<<<<<<<< changed
	Sleep %showTime%
	Gui, 1: Destroy
}

Esc::
EOF:
ExitApp
freiheit
Posts: 7
Joined: 16 Oct 2020, 23:37

Re: Having trouble positioning text in gui

14 Jun 2021, 15:00

Thank you, that gives me something to go on.

The reason I was calculating the gui's size was to try and force it to be wide enough for whatever the longest line of text (the gui text controls) needed and center every text control. I think I can make this work with your method.
freiheit
Posts: 7
Joined: 16 Oct 2020, 23:37

Re: Having trouble positioning text in gui

15 Jun 2021, 20:31

I manged to make all rows (text controls) be of uniform width which allowed me to have all text on the overlay GUI be centered. A few additional minor tweaks for my own needs and my finished code is below if anyone wishes to use it for their own purposes or further expand on it.

Code: Select all

#NoEnv
#SingleInstance force
#Persistent
OnExit, EOF   ; register OnExit
DetectHiddenWindows, On
SetTitleMatchMode, 2
SetWorkingDir %A_ScriptDir%

; display the current time and date when pressing Shift+RightCtrl
+RCtrl::  ; Right Windows key
	; Setup overlay window
	WindowID := Overlay_CreateGUI()

	; Setup row to display current time
	FormatTime, nowtime, , h:mm tt
	ControlID := Overlay_CreateRow(72, nowTime)

	; Setup row to display current date
	FormatTime, nowdate, , MMM dd, yyyy
	ControlID := Overlay_CreateRow(40, nowDate)

	; Make GUI control widths equal so all text can be centered
	Overlay_UniformCtrlWidths()

	; Show overlay window
	Overlay_ShowGUI(667)
Return

Return

Overlay_CreateGUI(LRM:=16, TBM:=4) {  ; LRM: left & right margin, TBM: top & bottom margin
	BGC = 333333

	Gui, 1: +AlwaysOnTop +ToolWindow -Caption +E0x20 ; Click through GUI always on top.
	Gui, 1: Color, %BGC%
	Gui, 1: Margin, %LRM%, %TBM%
	Gui, 1: +LastFound
	WinSet, Transparent, 160
	OverlayWin := WinExist()
	Return OverlayWin
}

Overlay_CreateRow(fontSize, textToShow) {
	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, xm y+0 center %FGC% BackgroundTrans, %textToShow%     ; <<<<<<<<<< changed
	GuiControlGet, thisCtrl, hwnd, %textToShow%
	Return thisCtrl
}


; Set width of all gui controls to widest control
Overlay_UniformCtrlWidths() {
	CtrlWidth := 0  ; set variable to hold widest control found so far
	WinGet, cList, ControlListHwnd, %WindowID%
	; Loop through once to find the widest button
	Loop, Parse, cList, "`n"
	{
		GuiControlGet, thisCtrl, Pos, %A_LoopField%
		If (thisCtrlW > CtrlWidth) {
			CtrlWidth := thisCtrlW + 5
		}
	}

	; Loop through second time to set all buttons to same width
	Loop, Parse, cList, "`n"
	{
		GuiControl, Move, %A_LoopField%, w%CtrlWidth%
	}
}

; show the final gui with both text controls (set some reasonable default values for size)
Overlay_ShowGUI(showTime=1000) {
	Gui, 1: Show, NoActivate
	Sleep %showTime%
	Gui, 1: Destroy
}

Esc::
EOF:
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], TAC109 and 259 guests