2014-09-10 173 views
1

我想列出进程的所有窗口,例如Word。这只给我主窗口:在PowerShell中获取进程的所有窗口

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

我想在这里列出Document1。

ID名称MainWindowTitle


1616 WINWORD文档2 - Microsoft Word中

有没有什么办法来访问除主要的一个其他窗口?

+1

您可以使用[EnumWindows的(http://stackoverflow.com/questions/820909/get-applications-window-handles)函数。但是,这是Win32代码而不是.NET代码,所以它可能不是最直接的方法。请记住,[许多应用程序使用隐藏窗口](http://stackoverflow.com/questions/2970184/issue-with-enumwindows)。我不知道任何PowerShell或.NET本地方法。 – 2014-09-10 17:34:53

回答

0

感谢培根建议我设法找到一个解决方案,但如果你有任何低于此麻烦,请您分享:

<# 
.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}}