本文介绍了如何通过进程名称将焦点带到窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正确理解这一点,此代码应该捕获活动窗口并使其保持焦点.concentr.exe 是进程名称.如何根据进程名称使窗口成为焦点?

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 based on process name?

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);
 }

推荐答案

我找到了:

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)
}

这篇关于如何通过进程名称将焦点带到窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:32