I was wondering if I could contribute this directly to GitHub, or if someone else could trans-literate the code?
Code: Select all
GetMonitorFromHwnd(winId)
{
/*
This is optional code. Commenting out due to irrelevance to the current post
if !WinExist("ahk_id " . winId)
{
return false
}
else if (!RegExMatch(winId, Runtime.hwnd_regex))
{
Throw Exception("Invalid window id passed: " . winId)
}
*/
mCount := MonitorGetCount()
if (mCount = 1)
{
/*
If there is only one monitor, there is only one answer.
moveWinToScreen is also assuming more than one monitor.
*/
return 1
}
/*
Refer to microsoft documentation for user32.dll method MonitorFromWindow
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-monitorfromwindow
MONITOR_DEFAULTTONULL = 0
MONITOR_DEFAULTTOPRIMARY = 1
MONITOR_DEFAULTTONEAREST = 2
*/
if !(monitorHandle := DllCall("User32.dll\MonitorFromWindow", "UInt", winId, "UInt", 2))
{
Throw Exception("Error in DllCall - Unable to set monitorHandle.")
}
/*
Refer to microsoft documentation for user32.dll method GetMonitorInfo
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getmonitorinfoa
BOOL GetMonitorInfoA(
HMONITOR hMonitor,
LPMONITORINFO lpmi
);
*/
VarSetCapacity(monitorInfo, 40)
NumPut(40, monitorInfo)
if (SubStr(A_OSVersion, 1, 2) = 10)
{
; Windows 10
DllCall("User32.dll\GetMonitorInfoA", "UInt", monitorHandle, "UInt", &monitorInfo)
}
else
{
; Not Windows 10
DllCall("User32.dll\GetMonitorInfo", "UInt", monitorHandle, "UInt", &monitorInfo)
}
; Create a bounding rectangle
monitorLeft := NumGet(monitorInfo, 4, "Int")
monitorTop := NumGet(monitorInfo, 8, "Int")
monitorRight := NumGet(monitorInfo, 12, "Int")
monitorBottom := NumGet(monitorInfo, 16, "Int")
Loop mCount
{
MonitorGet(A_Index, tempMonLeft, tempMonTop, tempMonRight, tempMonBottom)
if monitorLeft = tempMonLeft && monitorTop = tempMonTop && monitorRight = tempMonRight && monitorBottom = tempMonBottom
{
; Hey we found the monitor by bounding rectangle!
return A_Index
}
}
; A value is expected.
return false
; End of function :D
}