How should I calculate the visible screen width, taking DPI into account. If the GUI is too large for the screen, set a control's width to ensure all content is visible. Will these calculations work consistently across all resolutions and scaling settings? I have gone through this page, but it's difficult to comprehend. Thanks.
viewtopic.php?t=121040
Code: Select all
#Requires AutoHotkey v2
#SingleInstance
g := Gui()
str := 'Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string'
font := 'Segoe UI'
fontSize := 32
gWidth := ControlGetTextWidth(str, font, fontSize) + g.MarginX*2
; ???
switch A_ScreenDPI {
case 96: visibleScreenWidth := A_ScreenWidth
case 120: visibleScreenWidth:= A_ScreenWidth * 0.75
case 144: visibleScreenWidth := A_ScreenWidth * 0.65
; etc.
; default
}
if gWidth > visibleScreenWidth
txtWidth := visibleScreenWidth - g.MarginX*2
g.SetFont('s' fontSize, font)
g.Add('Text', (IsSet(txtWidth) ? 'w' txtWidth : ''), str)
g.Show()
ControlGetTextWidth(str:='', font:='', fontSize:='')
{
g := Gui()
g.SetFont('s' fontSize, font)
g.txt := g.Add('Text',, str)
g.Show('Hide')
g.txt.GetPos(,, &ctrlWidth)
g.Destroy()
return ctrlWidth
}