DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

Post your working scripts, libraries and tools
ahk7
Posts: 100
Joined: 06 Nov 2013, 16:35

DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

05 Oct 2017, 12:55

DPI() can either return the scaling factor or calculate position/values for AHK controls (font size, position (x y), width, height).

Code and Documentation

https://github.com/hi5/dpi

What is it?

At work I have the opportunity to work on a 4K monitor from time to time. I have quite a few scripts with GUIs and even though the scaling is set to 150% and AutoHotkey scales the GUI automatically - see https://autohotkey.com/docs/commands/Gui.htm#DPIScale - I still find it often too small / inconvenient to use.

As I didn't want to redo all the GUI sizes, control positions, etc or scale all the coordinates manually so the GUI would be usable on various DPI settings, I wrote a simple function to do all the work dynamically. I still have to modify the GUI code but now not that much editing is needed and it will scale the GUI depending on the DPI setting set by the user.

Example: here is a GUI at 144 DPI before and after applying dpi():
Image
(at 96 dpi the GUI will probably look huge)

If you want to disable the scaling you can set the dpi by simply calling the function see setdpi in the documentation.

Simple GUI code before DPI()

Code: [Select all] [Download] GeSHi © Codebox Plus

Gui, Font, s10
Gui, Add, Text, x5 y5, Hello
Gui, Add, Edit, xp yp+20 w200 h100 vVar, Goodbye
Gui, Add, Button, xp yp+110 w100 gMyLabel, OK
Gui, Add, Button, xp+110 yp w50 gGuiClose, Cancel
Gui, Show, w220 h200, DPI() test GUI
Return

You can simply call the dpi() function by wrapping it around the options like so:

Code: [Select all] [Download] GeSHi © Codebox Plus

Gui, Font, % dpi("s10")
Gui, Add, Text, % dpi("x5 y5"), Hello
Gui, Add, Edit, % dpi("xp yp+20 w200 h100 vVar"), Goodbye
Gui, Add, Button, % dpi("xp yp+110 w100 gMyLabel"), OK
Gui, Add, Button, % dpi("xp+110 yp w50 gGuiClose"), Cancel
Gui, Show, % dpi("w220 h200"), DPI() test GUI
Return
Last edited by ahk7 on 09 Oct 2017, 11:41, edited 1 time in total.
Helgef
Posts: 2482
Joined: 17 Jul 2016, 01:02
Contact:

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

05 Oct 2017, 17:10

Very convenient, it works well, thank you very much for sharing. :thumbup:
About the example in the readme, the code with using dpi() sets gui height 220, while the one without set height 180, so it will look like there is some poor scaling going on, but there isn't :D
You do not handle the case when height or width is -1, for example for the picture control, you can specify h-1 to keep aspect ratio. Quick fix:

Code: [Select all] [Download] GeSHi © Codebox Plus

RegExMatch(option,"\K((-|)\d+)",number)
newnumber:= number == -1 ? -1 : r ? Round(number*factor) : number*factor

Personal preference: instead of the global variable, I'd use a static variable which can be set, eg, via a third parameter, dpi(,,setDpi:=37).

Cheers.
just me
Posts: 4865
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

06 Oct 2017, 05:11

I almost ever used 96 DPI, so I don't know why you are dissatisfied with the built-in scaling.

A few remarks:
  • You should not scale the rows option.
  • You should elaborate the RegEx functions. Examples:

    Code: [Select all] [Download] GeSHi © Codebox Plus

    Gui, Add, Edit, % dpi("xp yp+20 w200 h100 vVar hwndEdit10ID"), Goodbye
    GuiControl, , MyPic, % dpi("*icon2 *w100 *h-1 C:\My Application.exe")
  • You should consider to return either Round() or Floor(), though AHK currently ignores the fractional part. The internal scaling is eqivalent to Round().

@Helgef: There are cases when -1 should be scaled dependent on the DPI, e.g. yp-1.
Remaining with AHK 1.1.25.02 until v2 will become beta.
Helgef
Posts: 2482
Joined: 17 Jul 2016, 01:02
Contact:

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

06 Oct 2017, 05:41

There are cases when -1 should be scaled dependent on the DPI, e.g. yp-1.

Good point :thumbup:

Also, you might want to consider tabs when parsing, probably space will suffice for99.99 % of the time, but it is easy to add,


Cheers.
ahk7
Posts: 100
Joined: 06 Nov 2013, 16:35

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

06 Oct 2017, 13:11

Thanks for the useful feedback which I will try to incorporate.

I've prepared a test script to see if it can correctly catch "all" options that need to be processed ",1" should be processed e.g. x5 -> x10 and ",0" should be skipped and kept as is e.g. hwndEdit10ID (should not be changed to hwndEdit20ID, good catch just me)
Do I miss something obvious?

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by ahk7 on 09 Oct 2017, 11:41, edited 1 time in total.
ahk7
Posts: 100
Joined: 06 Nov 2013, 16:35

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

06 Oct 2017, 16:14

A development branch is (temporarily) available here https://github.com/hi5/dpi/tree/v0.3

Changes
  • Replaced super global variable ###dpiset with static variable within dpi() to set dpi, usage: dpi(,120)
  • Removed r parameter, always use Round()
  • No longer scales the Rows option and others that should be skipped (h-1, *w0, hwnd etc)
ahk7
Posts: 100
Joined: 06 Nov 2013, 16:35

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

09 Oct 2017, 11:42

Merged the development branch, v0.31 is now the master branch https://github.com/hi5/dpi (refactored the code somewhat)
Helgef
Posts: 2482
Joined: 17 Jul 2016, 01:02
Contact:

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

17 Oct 2017, 02:50

Excellent updates, I really like it :thumbup:
User avatar
DataLife
Posts: 189
Joined: 29 Sep 2013, 19:52

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

17 Oct 2017, 07:58

Concerning your github example 3, on the DropDownList you did not add vVar inside the parenthesis.

Code: [Select all] [Download] GeSHi © Codebox Plus

Gui, Add, Edit,     % dpi("x25 yp-5 w300 h20 Number vVar"), EditText

SelectMenuPos:=2
Gui, Add, DropDownList, % dpi("xp yp+10 w200 h25 r4 Choose" SelectMenuPos) " vVar", 1 - Option|2 - Option|3 - Option
; Choose + the SelectMenuPos selects "2 - Option" in the DropDownList

Check:=1
Gui, Add, Checkbox, % dpi("x25 yp+20 w200 h16 Checked" Check " vVar"), Option
; Checked + the Check variable ticks the checkbox
Is vVar added within the parenthesis or not?
Check out my scripts. (MyIpChanger) (XPSnap) (SavePictureAs)
All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.
Helgef
Posts: 2482
Joined: 17 Jul 2016, 01:02
Contact:

Re: DPI() - writing friendlier DPI-Aware AutoHotkey GUIs

17 Oct 2017, 08:10

You can do it either way DataLife, the function DPI() will scale the relevant numbers in the option string, and leave the appropriate options alone, then return and the options will be concatenated. Leaving it outside means one less thing to parse in the function, you will never notice, but you might feel good about it :ugeek: .

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 10 guests