- 20240413_034627.png (463.45 KiB) Viewed 561 times
Sorry,
UIA cannot be used in this case.
It seems like the program was designed without considering it.
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
F5::msgbox GetWindowDpiAwarenessContextIgnoringInfoFlag(winExist("A")) ; #define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
GetWindowDpiAwarenessContextIgnoringInfoFlag(hWnd) {
AreDpiAwarenessContextsEqual(dpiContextA, dpiContextB) {
return dllCall("User32.dll\AreDpiAwarenessContextsEqual", "Ptr",dpiContextA, "Ptr",dpiContextB)
}
GetWindowDpiAwarenessContext(hWnd) {
return dllCall("User32.dll\GetWindowDpiAwarenessContext", "Ptr",hWnd, "Ptr")
}
if (dpiContextA:=getWindowDpiAwarenessContext(hWnd)) {
loop 5 {
dpiContextB:=-1*A_Index
if (areDpiAwarenessContextsEqual(dpiContextA,dpiContextB))
return dpiContextB
}
}
return false
}
When I ran it, I found that the GUI windows were much blurrier than expected.
Just to be sure, I checked the value, and
DPI_AWARENESS_CONTEXT was
UNAWARE.
In this scenario, if the monitor scaling isn't at 100%, the drawn window itself stretches according to the ratio.
This makes it challenging for AHK, which uses
SYSTEM_AWARE, to perform image searches.
While it's possible to adjust AHK to
UNAWARE to attempt to resolve this, it's not a method I particularly prefer.
- 20240413_060404.png (571.83 KiB) Viewed 535 times
Code: Select all
/*
The libraries used in this code, excluding Gdip, have not been shared anywhere, so it's not executable.
I plan to share them in the future, but I'm currently too busy to do so.
*/
#Requires AutoHotkey v1.1
#SingleInstance Force
#Include <FunctionGroup_Gdip_Ex-1.54.0-ahk1>
#Include <FunctionGroup_mmikeww.Gdip_All-1.54.0-ahk1>
#Include <FunctionGroup_MonitorExGet-1.0.0-ahk1>
pToken:=Gdip_Startup()
onExit("exitFunc")
exitFunc(_*) {
global pToken
Gdip_Shutdown(pToken)
}
F5::
if (hWnd:=winExist("A")) {
pBitmap:=Gdip_DpiBitmapFromHWND(hWnd) ; PrintWindow
hBitmap:=Gdip_CreateHBITMAPFromBitmap(pBitmap)
splashImage % "HBITMAP:" hBitmap
Gdip_DisposeImage(pBitmap)
}
return
The left side is the window captured (considering DPI) with Gdip, and the right side is the actual window.
It demonstrates that the graphics drawn are smaller in reality. Therefore, when performing image searches, using the image obtained through
PrintWindow, which is crisper than the actual window considering the enlargement and anti-aliasing, is preferable.
- 20240413_044351.png (632.82 KiB) Viewed 561 times
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
#Include <FunctionGroup_buliasz.Gdip_All-1.61.0-ahk2> ; #Include <Gdip_All> on your computer.
; AHKv2-Gdip
; 8960b875c4f1064865b51c72978d61e6648c0343
; https://github.com/buliasz/AHKv2-Gdip
pToken:=Gdip_Startup(), onExit((*)=>Gdip_Shutdown(pToken))
F5:: {
static DPI_AWARENESS_CONTEXT_UNAWARE:=-1
if !(hWnd:=winExist("A"))
return
if (getWindowDpiAwarenessContextIgnoringInfoFlag(hWnd)!==DPI_AWARENESS_CONTEXT_UNAWARE)
return
winGetPos(,, &width, &height, hWnd)
if (windowDpiScale:=dllCall("User32.dll\GetDpiForWindow", "Ptr",hwnd, "UInt")/A_ScreenDPI) ; In fact, I'm aware that there is a better API for this case, but since I'm still studying, I'll stick to the method I've been using so far for now.
width:=round(width*windowDpiScale), height:=round(height*windowDpiScale)
;------------------
hbm:=CreateDIBSection(Width, Height), hdc:=CreateCompatibleDC(), obm:=SelectObject(hdc, hbm)
PrintWindow(hwnd, hdc)
pBitmap:=Gdip_CreateBitmapFromHBITMAP(hbm)
SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
Gdip_SaveBitmapToFile(pBitmap, sOutput:=A_ScriptDir "\ChartImage-" A_Now ".png")
Gdip_DisposeImage(pBitmap)
;------------------
if (fileExist(sOutput))
run(sOutput)
}
; ahk1.1 ahk2.0
GetWindowDpiAwarenessContextIgnoringInfoFlag(hWnd) {
AreDpiAwarenessContextsEqual(dpiContextA, dpiContextB) {
return dllCall("User32.dll\AreDpiAwarenessContextsEqual", "Ptr",dpiContextA, "Ptr",dpiContextB)
}
GetWindowDpiAwarenessContext(hWnd) {
return dllCall("User32.dll\GetWindowDpiAwarenessContext", "Ptr",hWnd, "Ptr")
}
if (dpiContextA:=getWindowDpiAwarenessContext(hWnd)) {
loop 5 {
dpiContextB:=-1*A_Index
if (areDpiAwarenessContextsEqual(dpiContextA,dpiContextB))
return dpiContextB
}
}
return false
}
So, by capturing the image directly using
PrintWindow and saving it to a file, we can examine the state of the actual drawn graphics.
Our task is simply to extract the starting image for the image search from here.