【问题标题】:Determine which monitor the focus window is on?确定焦点窗口在哪个显示器上?
【发布时间】:2020-01-23 17:07:05
【问题描述】:

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

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

【问题讨论】:

    标签: autohotkey


    【解决方案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
            }
        }
    }
    

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

    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
            }
        }
    }
    

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

    编辑:

    如果你需要工作区(从工作区排除tarkbar)使用这个功能。

    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
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-24
        相关资源
        最近更新 更多