简体   繁体   English

获取PowerShell中进程的所有窗口

[英]Get all windows of a process in powershell

I want to list all windows of a process, say Word. 我想列出一个进程的所有窗口,比如Word。 This only gives me main window: 这只给了我一个主窗口:

Get-Process winword |where {$_.mainWindowTItle} |format-table id,name,mainwindowtitle –AutoSize

I want to also list Document1 here. 我还想在这里列出Document1。

Id Name MainWindowTitle Id名称MainWindowTitle


1616 WINWORD Document2 - Microsoft Word 1616 WINWORD Document2 - Microsoft Word

is there any way to access windows other than main one? 有没有办法访问除主要窗口以外的窗口?

Thanks to Bacon Bits suggestion I managed to find a solution, but if you have any less cumbersome than this, please share: 感谢培根比特的建议,我设法找到了解决方案,但如果你没有比这更麻烦,请分享:

<#
 .Synopsis
 Enumerieren der vorhandenen Fenster
#>

$TypeDef = @"

using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Api
{

 public class WinStruct
 {
   public string WinTitle {get; set; }
   public int WinHwnd { get; set; }
 }

 public class ApiDef
 {
   private delegate bool CallBackPtr(int hwnd, int lParam);
   private static CallBackPtr callBackPtr = Callback;
   private static List<WinStruct> _WinStructList = new List<WinStruct>();

   [DllImport("User32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   private static extern bool EnumWindows(CallBackPtr lpEnumFunc, IntPtr lParam);

   [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
   static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

   private static bool Callback(int hWnd, int lparam)
   {
       StringBuilder sb = new StringBuilder(256);
       int res = GetWindowText((IntPtr)hWnd, sb, 256);
      _WinStructList.Add(new WinStruct { WinHwnd = hWnd, WinTitle = sb.ToString() });
       return true;
   }   

   public static List<WinStruct> GetWindows()
   {
      _WinStructList = new List<WinStruct>();
      EnumWindows(callBackPtr, IntPtr.Zero);
      return _WinStructList;
   }

 }
}
"@

Add-Type -TypeDefinition $TypeDef -Language CSharpVersion3

[Api.Apidef]::GetWindows() | Where-Object { $_.WinTitle -like "*Word" } | Sort-Object -Property WinTitle | Select-Object WinTitle,@{Name="Handle"; Expression={"{0:X0}" -f $_.WinHwnd}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM