将窗口切换到其他屏幕并将窗口从其他屏幕切换到主屏幕

将窗口切换到其他屏幕并将窗口从其他屏幕切换到主屏幕

我希望发生的是,当我按下键盘组合键时,监视器 2 顶部的窗口转到监视器 1,而监视器 1 顶部的窗口转到监视器 2。

有人有一个这里有类似的问题。

那里的建议是创建一个脚本来帮你完成这个任务。我不知道该怎么做。如果有人能给我指明正确的方向,那就太好了。

答案1

Win+Arrow几次会将活动窗口移至另一台显示器。我认为它会尝试取消最大化和/或拆分它以显示屏幕。您可能还必须按Win+Up来最大化它

以下是一些脚本片段(powershell cmdlet),在您修改后,它们可以为您自动执行此操作。我没有的代码是它如何知道要切换哪两个窗口?它可以简单地假设它是当前窗口和 Alt+Tab 中的下一个窗口...

# https://docs.microsoft.com/en-us/windows/win32/shell/controlpanel-canonical-names
# https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=net-5.0
Add-Type -AssemblyName 'System.Windows.Forms'
# hits tab key five times, then enter key twice
[System.Windows.Forms.SendKeys]::SendWait("{TAB 5} {ENTER 2}")
Sleep 2 # wait 2sec then press Alt+F4
[System.Windows.Forms.SendKeys]::SendWait("%{F4}")

答案2

您需要使用第三方产品,例如免费的 自动热键

我在帖子中找到了一个 AutoHotkey 脚本,它可以在两个显示器之间切换所有窗口,甚至可以处理不同的显示器属性 在显示器之间交换所有窗口 - Windows 10 错误。此脚本注意不要将 Windows 10 任务栏从辅助显示器上移开。

我只修改了脚本,将切换屏幕的组合键改为Win+ S
由于我没有两台显示器,因此无法测试脚本。

我在这里重现该脚本:

; MonSwap - Swaps all the application windows from one monitor to another.
; v1.0.2
; Author: Alan Henager
;
; v1.0.1 - xenrik - Updated to use relative screen size when swapping
; v1.0.2 - boiler, masgo - exclude Windows 10 secondary monitor taskbar from being swapped

SetWinDelay, 0 ; This switching should be instant

; Set this key combination to whatever.
#s::
SwapAll:
{
  DetectHiddenWindows, Off ; I think this is default, but just for safety's sake...
  WinGet, WinArray, List ; , , , Sharp
  ; Enable the above commented out portion if you are running SharpE

  i := WinArray
  Loop, %i% {
     WinID := WinArray%A_Index%
     WinGetClass, ThisClass, ahk_id %WinID%
     if (ThisClass = "Shell_SecondaryTrayWnd") ; do not swap the secondary monitor taskbar
        continue
     WinGetTitle, CurWin, ahk_id %WinID%
     If (CurWin = ) ; For some reason, CurWin <> didn't seem to work.
     {}
     else
     {
        WinGet, IsMin, MinMax, ahk_id %WinID% ; The window will re-locate even if it's minimized
        If (IsMin = -1) {
           WinRestore, ahk_id %WinID%
           SwapMon(WinID)
           WinMinimize, ahk_id %WinID%
        } else {
           SwapMon(WinID)
        }
     }
  }
  return
}

SwapMon(WinID) ; Swaps window with and ID of WinID onto the other monitor
{
  SysGet, Mon1, Monitor, 1
  Mon1Width := Mon1Right - Mon1Left
  Mon1Height := Mon1Bottom - Mon1Top

  SysGet, Mon2, Monitor, 2
  Mon2Width := Mon2Right - Mon2Left
  Mon2Height := Mon2Bottom - Mon2Top

  WinGetPos, WinX, WinY, WinWidth, WinHeight, ahk_id %WinID%
  WinCenter := WinX + (WinWidth / 2)
  if (WinCenter >= Mon1Left and WinCenter <= Mon1Right) {
    
    NewX := (WinX - Mon1Left) / Mon1Width
    NewX := Mon2Left + (Mon2Width * NewX)

    NewWidth := WinWidth / Mon1Width
    NewWidth := Mon2Width * NewWidth

    NewY := (WinY - Mon1Top) / Mon1Height
    NewY := Mon2Top + (Mon2Height * NewY)

    NewHeight := WinHeight / Mon1Height
    NewHeight := Mon2Height * NewHeight
 
 } else {
    NewX := (WinX - Mon2Left) / Mon2Width
    NewX := Mon1Left + (Mon1Width * NewX)

    NewWidth := WinWidth / Mon2Width
    NewWidth := Mon1Width * NewWidth

    NewY := (WinY - Mon2Top) / Mon2Height
    NewY := Mon1Top + (Mon1Height * NewY)

    NewHeight := WinHeight / Mon2Height
    NewHeight := Mon1Height * NewHeight
  }

  WinMove, ahk_id %WinID%, , %NewX%, %NewY%, %NewWidth%, %NewHeight%
  return
}

安装 AutoHotKey 后,将脚本存储在一个.ahk文件中并双击它进行测试。您可以通过右键单击托盘栏中的绿色 H 图标并选择退出来停止脚本。要让它在登录时运行,请将其放在 的启动组中 C:\Users\USER-NAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

有用的 AutoHotkey 文档:

相关内容