是否可以使用batch / powershell脚本在监视器上打开和对齐4个窗口?


0

在Windows10上,我可以在一个虚拟桌面上手动打开并对齐最多4个窗口:

4个窗口在屏幕上对齐

我经常这样做,我甚至在屏幕上有两层,一层有文件夹,另一层有PowerShell控制台,指向同一个磁盘位置。

问题是,每当windows10有一个适合并想要重新启动时,我必须重新打开大约十几个文件夹和控制台才能继续工作。虽然这是常规的,并且几乎不需要花费几分钟,但它仍然需要几分钟的时间,而且我觉得应该是自动化的可怕任务。

:有没有办法使用批处理或PowerShell脚本自动化这个打开和对齐窗口?


Answers:


0

有很多解决方案,但这个代码有助于在没有第三方工具的情况下实现这一目标。遗憾的是,由于密钥不可用,我们无法使用SendKeysWin

这有点hacky。理想情况下,您需要查询目标监视器的分辨率,并使用所需的像素位置开始每个进程。

# stuff needed to send keystrokes
$source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeyboardSend
{
    public class KeyboardSend
    {
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        private const int KEYEVENTF_EXTENDEDKEY = 1;
        private const int KEYEVENTF_KEYUP = 2;
        public static void KeyDown(Keys vKey)
        {
            keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
        }
        public static void KeyUp(Keys vKey)
        {
            keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
        }
    }
}
"@

Add-Type -TypeDefinition $source -ReferencedAssemblies "System.Windows.Forms"

# the arrow key combinations (0 = Left Up, 1 = Left Down, 2 = Right Up, 3 = Right Down)
$LR = @('Left','Left','Right','Right')
$UD = @('Up','Down','Up','Down')

# edit the sleep value as needed.
$sleepMS = 200

# start a process, move it in the next desirable position, x4
0..3 | % {

    Start-Process powershell
    # if we don't wait for the process to open, we might not have focus.
    Sleep -Milliseconds $sleepMS
    [KeyboardSend.KeyboardSend]::KeyDown("LWin")
    [KeyboardSend.KeyboardSend]::KeyDown($LR[$_])
    Sleep -Milliseconds $sleepMS
    [KeyboardSend.KeyboardSend]::KeyDown($UD[$_])
    [KeyboardSend.KeyboardSend]::KeyUp("LWin")
    Sleep -Milliseconds $sleepMS
    [KeyboardSend.KeyboardSend]::KeyDown("Escape")

}

可用密钥列表

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.