1

I have been using the script Advanced Window Snap, which snaps windows to various places of the monitor, which uses the function GetMonitorIndexFromWindow to determine in which monitor a window is located.

I am converting it to AHKv2.

So far, I could get this:

GetMonitorIndexFromWindow(windowHandle) {
    ; Starts with 1.
    monitorIndex := 1

; Original
;    VarSetCapacity(&monitorInfo, 40)
; My try
    monitorInfo := Buffer(40)
    NumPut("Uint", 40, monitorInfo)

    if (monitorHandle := DllCall("MonitorFromWindow", "uint", windowHandle, "uint", 0x2))
        && DllCall("GetMonitorInfoA", "uint", monitorHandle, "uint", &monitorInfo) {
        monitorLeft   := NumGet(monitorInfo,  4, "Int")
        monitorTop    := NumGet(monitorInfo,  8, "Int")
        monitorRight  := NumGet(monitorInfo, 12, "Int")
        monitorBottom := NumGet(monitorInfo, 16, "Int")
        workLeft      := NumGet(monitorInfo, 20, "Int")
        workTop       := NumGet(monitorInfo, 24, "Int")
        workRight     := NumGet(monitorInfo, 28, "Int")
        workBottom    := NumGet(monitorInfo, 32, "Int")
        isPrimary     := NumGet(monitorInfo, 36, "Int") & 1

        monitorCount := MonitorGetCount

        Loop %monitorCount% {
            tempMon := MonitorGet(%A_Index%, &tempMonLeft, &tempMonTop, &tempMonRight, &tempMonBottom)

            ; Compare location to determine the monitor index.
            if ((monitorLeft = tempMonLeft) and (monitorTop = tempMonTop)
                and (monitorRight = tempMonRight) and (monitorBottom = tempMonBottom)) {
                monitorIndex := A_Index
                break
            }
        }
    }
    return monitorIndex
}

but it doesn't work because the way I replaced VarSetCapacity(monitorInfo, 40) doesn't seem to be what the DllCall expects.

I also tried strings, or buffers, but the DllCall (the second one) complains about "Error: Expected a Number but got a VarRef.".

In fact, I'm not even sure this DllCall is still needed, maybe the functions https://www.autohotkey.com/docs/v2/lib/Monitor.htm could be used to skip it altogether.

How can I convert the function to a syntax compatible with AHK v2?

3

0

You must log in to answer this question.

Browse other questions tagged .