2017-03-02 70 views
1

如果我正確理解這個代碼應該捕獲活動窗口並保持焦點。 concentr.exe是進程名稱。 如何根據進程名稱爲焦點窗口?如何通過流程名稱將焦點移至窗口?

Add-Type @" 
using System; 
using System.Runtime.InteropServices; 
public class UserWindows { 
[DllImport("user32.dll")] 
public static extern IntPtr GetForegroundWindow(); 
} 
"@    
try {    
$ActiveHandle = [UserWindows]::GetForegroundWindow() 
$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle}    
$Process | Select ProcessName, @{Name="concentr.exe";Expression= {($_.MainWindowTitle)}}    
} catch {    
Write-Error "Failed to get active Window details. More Info: $_"    
} 

我也試過

param([string] $proc="Citrix Connection Manager", [string]$adm) 
cls 

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" #-Verb runAs 
} 
else 
{ 
    $h = $p.MainWindowHandle 

[void] [WinAp]::SetForegroundWindow($h) 
[void] [WinAp]::ShowWindow($h,3); 
} 
+0

你沒有正確地理解這一點。 –

+0

好的,請幫我理解 – user770022

+0

'GetForegroundWindow()'獲得當前前景窗口的句柄,它不會*做*任何事情 –

回答

2

我使用這個腳本來做到這一點。修改爲你需要...

顯示,WindowByName

#Requires -RunAsAdministrator 

[CmdletBinding()] 
param (
    [string] 
    $ProcessNameRegEx = 'pow', 

    [string] 
    $WindowTitleRegEx = 'json' 
) 

$cs = @" 
using System; 
using System.Runtime.InteropServices; 

namespace User32 
{ 
    public static class WindowManagement 
    { 
     [DllImport("user32.dll", EntryPoint = "SetWindowPos")] 
     public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); 

     public const int SWP_NOSIZE = 0x01, SWP_NOMOVE = 0x02, SWP_SHOWWINDOW = 0x40, SWP_HIDEWINDOW = 0x80; 

     public static void SetWindowPosWrappoer(IntPtr handle, int x, int y, int width, int height) 
     { 
      if (handle != null) 
      { 
       SetWindowPos(handle, 0, x, y, 0, 0, SWP_NOSIZE | SWP_HIDEWINDOW); 

       if (width > -1 && height > -1) 
        SetWindowPos(handle, 0, 0, 0, width, height, SWP_NOMOVE); 

       SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW); 
      } 
     } 

     [DllImport("user32.dll", EntryPoint = "ShowWindow")] 
     public static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow); 

     public static void ShowWindowWrapper(IntPtr handle, int nCmdShow) 
     { 
      if (handle != null) 
      { 
       ShowWindow(handle, nCmdShow); 
      } 
     } 

     [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")] 
     public static extern IntPtr SetForegroundWindow(IntPtr hWnd); 

     public static void SetForegroundWindowWrapper(IntPtr handle) 
     { 
      if (handle != null) 
      { 
       SetForegroundWindow(handle); 
      } 
     } 
    } 
} 
"@ 

Add-Type -TypeDefinition $cs -Language CSharp -ErrorAction SilentlyContinue 


function Move-Window 
{ 
    param (
     [int]$MainWindowHandle, 
     [int]$PosX, 
     [int]$PosY, 
     [int]$Height, 
     [int]$Width 
    ) 

    if($MainWindowHandle -ne [System.IntPtr]::Zero) 
    { 
     [User32.WindowManagement]::SetWindowPosWrappoer($MainWindowHandle, $PosX, $PosY, $Width, $Height); 
    } 
    else 
    { 
     throw "Couldn't find the MainWindowHandle, aborting (your process should be still alive)" 
    } 
} 


function Show-Window 
{ 
    param (
     [int]$MainWindowHandle, 
     [int]$CmdShow 
    ) 

    if($MainWindowHandle -ne [System.IntPtr]::Zero) 
    { 
     [User32.WindowManagement]::ShowWindowWrapper($MainWindowHandle, $CmdShow); 
     [User32.WindowManagement]::SetForegroundWindowWrapper($MainWindowHandle); 
    } 
    else 
    { 
     throw "Couldn't find the MainWindowHandle, aborting (your process should be still alive)" 
    } 
} 


$windows = Get-Process | ? {$_.ProcessName -match $ProcessNameRegEx -and $_.MainWindowTitle -match $WindowTitleRegEx} | Select -Last 100 | Select Id, MainWindowTitle, MainWindowHandle | Sort MainWindowTitle 

$h = 180 
$w = 1500 
$x = 400 
$y = 800 
$deltax = 80 
$deltay = 180 

foreach ($window in $windows) 
{ 
    Move-Window $window.MainWindowHandle $x $y $h $w 
    Show-Window $window.MainWindowHandle 5 
    #$x -= $deltax 
    $y -= $deltay 
} 
+0

嗯,我沒有能夠得到這個工作。我想我需要做的唯一的變化是 參數( [字符串] $ ProcessNameRegEx =「concentr.exe」, [字符串] $ WindowTitleRegEx =「思傑連接管理器」 – user770022

+0

我要猜「 。'在你的輸入中的解釋不是你所期望的,因爲這是一個正則表達式。 –

1

我發現它

param([string] $proc="C:\Program Files (x86)\Citrix\ICA Client\concentr.exe", [string]$adm) 
cls 

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" #-Verb runAs 
} 
else 
{ 
$h = $p.MainWindowHandle 

[void] [WinAp]::SetForegroundWindow($h) 
[void] [WinAp]::ShowWindow($h,3); 
}