确定焦点窗口在哪个监视器上?

Anu*_*Anu 5 autohotkey

用于确定焦点窗口在哪个监视器上的小型 AHK 函数。

我正在编写一个脚本,该脚本需要焦点窗口所在监视器的上下文。我找到了很多解决方案,但没有一个太容易遵循,或者比需要的更复杂。

Anu*_*Anu 1

下面将为您介绍这一点。AHK 中的监控索引可供您参考。

GetFocusWindowMonitorIndex(){
    ;Get number of monitor
    SysGet, monCount, MonitorCount
    
    ;Iterate through each monitor
    Loop %monCount%{
        ;Get Monitor working area
        SysGet, workArea, Monitor, % A_Index
        
        ;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 % A_Index
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意下面是一个修改版本,其中窗口作为参数传递,而不是默认的;焦点窗口

GetFocusWindowMonitorIndex(thisWindow){
    ;Get number of monitor
    SysGet, monCount, MonitorCount
    
    ;Iterate through each monitor
    Loop %monCount%{
        ;Get Monitor working area
        SysGet, workArea, Monitor, % A_Index
        
        ;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 % A_Index
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

即使这可以帮助另一个人,我也会称之为胜利。

编辑:

如果您需要工作区域(从工作区域中排除 tarbar),请使用此功能。

GetFocusWindowMonitorIndex(){
    ;Get number of monitor
    SysGet, monCount, MonitorCount
    
    ;Iterate through each monitor
    Loop %monCount%{
        ;Get Monitor working area
        SysGet, workArea, MonitorWorkArea , % A_Index
        
        ;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 % A_Index
        }
    }
}
Run Code Online (Sandbox Code Playgroud)