3

如何获取某个进程的窗口类名?我想在 c# 中实现这一点。

我已经在c#中尝试过进程类,但我只能获取进程的窗口名称。

谢谢

4

2 回答 2

7

我假设您的意思是要获取进程主窗口的类名。

为此,您需要使用对象的 获取主窗口的句柄MainWindowHandleProcess然后使用以下互操作方法获取类名:

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

有关示例代码,请参阅pinvoke.net ,有关该功能的详细信息,请参阅MSDN

于 2012-09-11T14:56:35.187 回答
1

您也可以使用windows ui 自动化框架来实现这一点,而无需进入 pinvoke。

        int pidToSearch = 316;
        //Init a condition indicating that you want to search by process id.
        var condition = new PropertyCondition(AutomationElementIdentifiers.ProcessIdProperty, 
            pidToSearch);
        //Find the automation element matching the criteria
        AutomationElement element = AutomationElement.RootElement.FindFirst(
            TreeScope.Children, condition);

        //get the classname
        var className = element.Current.ClassName;
于 2012-09-11T15:07:19.190 回答