2017-03-02 16 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

OKを教えてください。 – 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

うーん、私はこれを動作させることができなかったhavent。私は私が作る必要がある唯一の変更は のparam( [文字列] $ ProcessNameRegEx = 'concentr.exe'、 [文字列] $ WindowTitleRegEx = 'Citrixコネクションマネージャ' – 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); 
} 
関連する問題