如何从c#获取进程窗口类名?
如何获取某个进程的窗口类名? 我想在c#中实现这一点。
我在c#中尝试了进程类,但我只能得到进程的窗口名称。
谢谢
我假设你的意思是你想得到一个进程主窗口的类名。
为此,您需要使用Process
对象的MainWindowHandle
获取主窗口的句柄,然后使用以下interop方法获取类名:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
有关该函数的详细信息,请参阅pinvoke.net以获取示例代码和MSDN 。
您也可以使用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;