Run the following scripts in a computer where the screen scaling is higher than 100% (in my computer, 150%). F1 moves the cursor to incorrect coordinates, F2 to correct coordinates:
TestGui.ahk:
Code: Select all
#Requires AutoHotkey v2
CreateGui(A_Args.Length ? A_Args[1] : -1)
CreateGui(context) {
static g
if IsSet(g)
g.Destroy()
DllCall("SetThreadDpiAwarenessContext", "ptr", context, "ptr")
g := Gui()
g.AddText(, "DPIAwareness test GUI")
g.OnEvent("Close", (*) => ExitApp())
g.AddEdit("w300 h200", "`n`n`n`nPress F1 to move mouse to caret")
g.AddText(,"DPI awareness: ")
g.AddRadio("YP" (context = -1 ? " Checked" : ""), "Unaware").OnEvent("Click", (*) => (Run('"' A_AhkPath '" "' A_ScriptFullPath '" -1'), ExitApp()))
g.AddRadio("YP" (context = -2 ? " Checked" : ""), "System").OnEvent("Click", (*) => (Run('"' A_AhkPath '" "' A_ScriptFullPath '" -2'), ExitApp()))
g.AddRadio("YP" (context = -3 ? " Checked" : ""), "Per-monitor").OnEvent("Click", (*) => (Run('"' A_AhkPath '" "' A_ScriptFullPath '" -3'), ExitApp()))
g.Show()
}
Code: Select all
#Requires AutoHotkey v2
CoordMode "Caret", "Screen"
CoordMode "Mouse", "Screen"
F1::CaretGetPos(&caretX, &caretY), MouseMove(caretX, caretY)
F2::{
CaretGetPos(&caretX, &caretY)
if (A_ScreenDPI != 96 && !DllCall("GetAwarenessFromDpiAwarenessContext", "ptr", DllCall("GetWindowDpiAwarenessContext", "ptr", hWnd := WinExist("A"), "ptr"), "int")) { ; if DPI-unaware then caret coordinates are system DPI until the the client area of the control in which the caret is located in, at which point the DPI is 96
WinGetPos(&ctrlX, &ctrlY,,, ControlGetFocus())
caretX := ctrlX + (caretX - ctrlX) * A_ScreenDPI // 96
, caretY := ctrlY + (caretY - ctrlY) * A_ScreenDPI // 96
}
MouseMove(caretX, caretY)
}