簡體   English   中英

如何使用 Powershell 在顯示器的特定位置打開應用程序

[英]How can i open an application in specific places on my monitors, with Powershell

這是我拼湊起來的,我不明白為什么它不起作用。 腳本的意思是它應該打開google-chrome,並將其放置在屏幕的右側。

我不是最擅長 powershell,所以我希望你們能幫助我理解為什么它不起作用。 並幫助我找到解決方案。

$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"

Function Win ($Key)
{
    [KeyboardSend.KeyboardSend]::KeyDown("LWin")
    [KeyboardSend.KeyboardSend]::KeyDown("$Key")
    [KeyboardSend.KeyboardSend]::KeyUp("LWin")

}


$chrome = 'C:\Program Files\Google\Chrome\Application\chrome.exe' 
$url = 'www.google.com'
& $chrome $url

$wshell = New-Object -ComObject wscript.shell;

[KeyboardSend.KeyboardSend]::KeyDown("LWin")
sleep 1
$wshell.SendKeys('{RIGHT}')


[KeyboardSend.KeyboardSend]::KeyUp("LWin")

#1:您似乎在嘗試多種方法來完成相同的功能。 例如Win()函數、 $wshell.SendKeys和 C# 中的KeyUp/KeyDown

為了解決這個問題,我們將引入一個簡單的新 C# 函數來替換 SendKeys:

public static void KeyPress(Keys vKey)
{
    KeyDown(vKey);
    KeyUp(vKey);
}

所以現在你可以像這樣發送你的Win + Right

KeyDown("LWin");
KeyPress("Right");
KeyUp("LWin");

#2:要向應用程序發送密鑰,Windows 要求應用程序窗口已准備好並在前台處於活動狀態。 如果您在執行后嘗試發送太快,它將不起作用。 如果您在窗口處於后台時嘗試,它將不起作用。

為了解決這個問題,我們將使用 Windows API:

[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr hWnd);
public static bool SetForeground(string processName)
{
    Process proc = null;
    var procs = Process.GetProcessesByName(processName);
    // Wait until a window handle is available.
    // Some apps like Chrome spawns MANY processes, but only
    // one will have a MainWindowHandle.
    System.Threading.Tasks.Task.Run(() =>
    {
        while (proc == null)
            proc = procs.FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero);
    }).Wait(2000); // Quit if none found in this time
    if (proc == null) return false;
    SetForegroundWindow(proc.MainWindowHandle);
    return true;
}

你可以像這樣使用它:

SetForeground("chrome");

現在是完整的代碼。

注意:我使用的是msedge因為我沒有安裝 chrome。 您應該能夠找到對 edge 的 2 個引用並將它們都替換為chrome

$source = @"
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeyboardSend
{
public class KeyboardSend
{
    private const int KEYEVENTF_EXTENDEDKEY = 1;
    private const int KEYEVENTF_KEYUP = 2;
    [DllImport("user32.dll")]
    public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
    [DllImport("User32.dll")]
    static extern int SetForegroundWindow(IntPtr hWnd);
    public static bool SetForeground(string processName)
    {
        Process proc = null;
        var procs = Process.GetProcessesByName(processName);
        // Wait until a window handle is available.
        // Some apps like Chrome spawns MANY processes, but only
        // one will have a MainWindowHandle.
        System.Threading.Tasks.Task.Run(() =>
        {
            while (proc == null)
                proc = procs.FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero);
        }).Wait(2000); // Quit if none found in this time
        if (proc == null) return false;
        SetForegroundWindow(proc.MainWindowHandle);
        return true;
    }
    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);
    }
    public static void KeyPress(Keys vKey)
    {
        KeyDown(vKey);
        KeyUp(vKey);
    }
}
}
"@

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

# Start your app
$chrome = 'msedge.exe' #change to chrome
$url = 'about:blank'
Start-Process -PassThru $chrome $url 

# Wait for window to be available and pull to front.
[KeyboardSend.KeyboardSend]::SetForeground("msedge"); #change to chrome
# May wanna wrap in an IF statement in case it failed.

# Send your keys
[KeyboardSend.KeyboardSend]::KeyDown("LWin");
[KeyboardSend.KeyboardSend]::KeyPress("Right");
[KeyboardSend.KeyboardSend]::KeyUp("LWin");

# You may need to send escape if Windows10 asks you 
# what you want to snap to the other side.
sleep -Milliseconds 200
[KeyboardSend.KeyboardSend]::KeyPress("Escape");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM