How to bring focus to window by process name?

后端 未结 4 1050
梦毁少年i
梦毁少年i 2020-12-16 22:47

If I\'m understanding this correctly this code should capture the active window and keep it in focus. concentr.exe is the process name. How do I bring a window in focus base

4条回答
  •  一向
    一向 (楼主)
    2020-12-16 22:59

    I found it:

    Param(
        [string] $proc="C:\Program Files (x86)\Citrix\ICA Client\concentr.exe",
        [string] $adm
    )
    Clear-Host
    
    Add-Type @"
        using System;
        using System.Runtime.InteropServices;
        public class WinAp {
          [DllImport("user32.dll")]
          [return: MarshalAs(UnmanagedType.Bool)]
          public static extern bool SetForegroundWindow(IntPtr hWnd);
    
          [DllImport("user32.dll")]
          [return: MarshalAs(UnmanagedType.Bool)]
          public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        }
    "@
    $p = Get-Process | Where {$_.mainWindowTitle} |
        Where {$_.Name -like "$proc"}
    if (($p -eq $null) -and ($adm -ne "")) {
        Start-Process "$proc" -Verb runAs
    } elseif (($p -eq $null) -and ($adm -eq "")) {
        Start-Process "$proc"
    } else {
        $h = $p.MainWindowHandle
        [void] [WinAp]::SetForegroundWindow($h)
        [void] [WinAp]::ShowWindow($h, 3)
    }
    

提交回复
热议问题