确定焦点窗口在哪个显示器上?



小型 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(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(){
;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
}
}
}

我想指出,如果出现以下情况,以前的答案将不起作用:

  1. 您有一个最大化的窗口(窗口将略微超出显示器边界,在我的情况下每个方向上都会超出 10 像素(
  2. 窗口重叠多个窗口。在这种情况下,最合适的做法是查看哪个监视器包含活动窗口的最大部分。
GetFocusWindowMonitorIndex(){
;Get number of monitor
SysGet, monCount, MonitorCount
;Get the position of the focus window
WinGetPos, WindowX, WindowY, WindowWidth, WindowHeight, A
;Make an array to hold the sub-areas of the window contained within each monitor
monitorSubAreas := []
;Iterate through each monitor
Loop %monCount%{
;Get Monitor working area
SysGet, Monitor, MonitorWorkArea , % A_Index

;Calculate sub-area of the window contained within each monitor
xStart := max(WindowX,  MonitorLeft)
xEnd :=  min(WindowX + WindowWidth,  MonitorRight)
yStart := max(WindowY, MonitorTop)
yEnd :=  min(WindowY + WindowHeight,  MonitorBottom)
area := (xEnd - xStart) * (yEnd - yStart)

;Remember these areas, and which monitor they were associated with
monitorSubAreas.push({"area": area, "index": A_Index})
}
;If there is only one monitor in the array, then you already have your answer
if(monitorSubAreas.length() == 1) {
return monitorSubAreas[1].index
}


;Otherwise, loop to figure out which monitor's recorded sub-area was largest
winningMonitor := 0
winningArea := 0
for index, monitor in monitorSubAreas {
winningMonitor := monitor.area > winningArea ? monitor.index : winningMonitor
winningArea := monitor.area > winningArea ?  monitor.area : winningArea
}
return winningMonitor
}

最新更新