Определить, на каком мониторе окно фокусировки включено? - PullRequest
4 голосов
/ 23 января 2020

Маленькая функция AHK, чтобы определить, на каком мониторе находится окно фокусировки.

Я писал сценарий, который нуждался в контексте, на котором был включен монитор. Я нашел довольно много решений, но ни одно из них не было слишком легким для подражания или было немного сложнее, чем нужно.

1 Ответ

0 голосов
/ 10 марта 2020

Ниже вы получите именно это. Индекс монитора в AHK, чтобы вы могли ссылаться на него.

GetFocusWindowMonitorIndex(){
    ;Get number of monitor
    SysGet, monCount, MonitorCount

    ;Counter
    i := 1

    ;Iterate through each monitor
    Loop %monCount%{
        ;Get Monitor working area
        SysGet, workArea, Monitor, %i%

        ;Get the position of the focus window
        WinGetPos, X, Y, , , A

        ;Check if the focus window in on the current monitor index
        if (X >= workAreaLeft && X < workAreaRight && Y >= workAreaTop && Y < workAreaBottom ){
            ;Return the monitor index since its within that monitors borders.
            return i
        }
        ;Next monitor
        i := i + 1
    }
}

Примечание. Ниже приведена измененная версия, в которой окно передается в качестве аргумента, а не по умолчанию; окно фокуса

GetFocusWindowMonitorIndex(thisWindow){
    ;Get number of monitor
    SysGet, monCount, MonitorCount

    ;Counter
    i := 1

    ;Iterate through each monitor
    Loop %monCount%{
        ;Get Monitor working area
        SysGet, workArea, Monitor, %i%

        ;Get the position of the focus window
        WinGetPos, X, Y, , , %thisWindow%

        ;Check if the focus window in on the current monitor index
        if (X >= workAreaLeft && X < workAreaRight && Y >= workAreaTop && Y < workAreaBottom ){
            ;Return the monitor index since it's within that monitors borders.
            return i
        }
        ;Next monitor
        i := i + 1
    }
}

Даже если это поможет еще одному человеку, я назову это победой.

РЕДАКТИРОВАТЬ:

Если вам нужна рабочая зона (за исключением tarkbar из рабочей области) используйте эти функции.

GetFocusWindowMonitorIndex(){
    ;Get number of monitor
    SysGet, monCount, MonitorCount

    ;Counter
    i := 1

    ;Iterate through each monitor
    Loop %monCount%{
        ;Get Monitor working area
        SysGet, workArea, MonitorWorkArea , %i%

        ;Get the position of the focus window
        WinGetPos, X, Y, , , A

        ;Check if the focus window in on the current monitor index
        if (X >= workAreaLeft && X < workAreaRight && Y >= workAreaTop && Y < workAreaBottom ){
            ;Return the monitor index since its within that monitors borders.
            return i
        }
        ;Next monitor
        i := i + 1
    }
}
...