Calculating visible screen width with DPI Considerations Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
XMCQCX
Posts: 258
Joined: 14 Oct 2020, 23:44

Calculating visible screen width with DPI Considerations

17 May 2024, 16:46

Hi,
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
}
Last edited by XMCQCX on 18 May 2024, 09:30, edited 1 time in total.
XMCQCX
Posts: 258
Joined: 14 Oct 2020, 23:44

Re: Calculating Text Control Width with DPI Considerations  Topic is solved

18 May 2024, 08:49

This was the simple calculation I was looking for. ChatGPT to the rescue!

Code: Select all

dpiScaling := A_ScreenDPI / 96
visibleScreenWidth := A_ScreenWidth / dpiScaling

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], Xeilous and 57 guests