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

Autohotkey 确定焦点窗口在哪个监视器上?,autohotkey,Autohotkey,小AHK功能,用于确定聚焦窗口打开的监视器 我正在编写一个脚本,它需要焦点窗口所在监视器的上下文。我找到了很多解决方案,但没有一个太容易理解,或者比需要的复杂一点。下面将为您提供这些解决方案。监视器索引使用AHK,因此您可以引用它 GetFocusWindowMonitorIndex(){ ;Get number of monitor SysGet, monCount, MonitorCount ;Iterate through each monitor

小AHK功能,用于确定聚焦窗口打开的监视器


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

下面将为您提供这些解决方案。监视器索引使用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
        }
    }
}
GetFocusWindowMonitorIndex(){
;获取监视器的编号
SysGet,monCount,MonitorCount
;遍历每个监视器
循环%monCount%{
;在工作区域设置监视器
系统获取、工作区、监视器%A\u索引
;获取焦点窗口的位置
Wingtpos,X,Y,A
;检查焦点窗口是否位于当前监视器索引上
如果(X>=工作区左侧和&X<工作区右侧和&Y>=工作区顶部和&Y<工作区底部){
;返回监视索引,因为它位于监视边界的范围内。
返回%A\u索引
}
}
}
下面的注释是一个修改版本,其中窗口作为参数传递,而不是默认值;焦点窗口

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
        }
    }
}
GetFocusWindowMonitorIndex(此窗口){
;获取监视器的编号
SysGet,monCount,MonitorCount
;遍历每个监视器
循环%monCount%{
;在工作区域设置监视器
系统获取、工作区、监视器%A\u索引
;获取焦点窗口的位置
WingtPos,X,Y,,%thisWindow%
;检查焦点窗口是否位于当前监视器索引上
如果(X>=工作区左侧和&X<工作区右侧和&Y>=工作区顶部和&Y<工作区底部){
;返回监视器索引,因为它位于监视器边界内。
返回%A\u索引
}
}
}
即使这对另外一个人有帮助,我也会称之为胜利

编辑:

如果您需要工作区(将柏油杆从工作区中排除),请使用此功能

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
        }
    }
}
GetFocusWindowMonitorIndex(){
;获取监视器的编号
SysGet,monCount,MonitorCount
;遍历每个监视器
循环%monCount%{
;在工作区域设置监视器
系统获取、工作区、监视器工作区,%A\u索引
;获取焦点窗口的位置
Wingtpos,X,Y,A
;检查焦点窗口是否位于当前监视器索引上
如果(X>=工作区左侧和&X<工作区右侧和&Y>=工作区顶部和&Y<工作区底部){
;返回监视索引,因为它位于监视边界的范围内。
返回%A\u索引
}
}
}