我正在构建一个应用程序,它给出了另一个应用程序的mainWindowhandle它收集有关窗口状态的信息。收集有关子窗口的信息我没有问题,但我无法访问应用程序的其他打开窗口,甚至无法访问菜单。有没有办法获得应用程序的所有窗口句柄?获取应用程序的窗口句柄
回答
你可以做Process.MainWindowHandle
似乎要做的事情:使用P/Invoke调用EnumWindows
函数,该函数为系统中的每个顶级窗口调用回调方法。
在您的回拨中,拨打GetWindowThreadProcessId
,并将窗口的进程ID与Process.Id
进行比较;如果进程ID匹配,则将窗口句柄添加到列表中。
首先,您必须获取应用程序主窗口的窗口句柄。
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr hWnd = (IntPtr)FindWindow(windowName, null);
然后,您可以使用该句柄来获取所有childwindows:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
private List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
梅茨问题ISN”我可以很容易地做到这一点,我不能做的是除了mainWindow和它的孩子以外的其他窗口... – user361526 2009-05-05 14:19:49
这适用于任何窗口,也适用于不属于自己的应用程序的窗口。对不起,如果我误解了你的问题。 – Mez 2009-05-05 18:56:33
从哪里来'EnumWindowProc'? – 2017-05-18 22:12:44
- 1. 获取SDL 2应用程序的窗口句柄
- 2. 获取窗口句柄
- 3. 使用窗口句柄获取IUKOWN
- 4. 如何获取当前C程序的父窗口的句柄?
- 5. 获取顶部窗口句柄(按Z索引排序窗口)
- 6. 获取C#中窗口的句柄#
- 7. 获取PyGI中的窗口句柄
- 8. 获取窗口的句柄在C#
- 9. 获取主窗口的句柄在WPF
- 10. 获取窗口的所有句柄
- 11. 获取的窗口句柄在Java中
- 12. 通过C++获取外部运行程序的窗口句柄
- 13. 如何获取另一个应用程序的所有窗口的句柄
- 14. 获取调用窗口的窗口句柄
- 15. 从另一个应用程序窗口获取专注控制的句柄
- 16. 在C#.net应用程序中获取创建窗口句柄的错误
- 17. ANSI C&WinAPI:如何从钩子程序获取窗口句柄?
- 18. Python Selenium获取当前窗口句柄
- 19. 如何获取桌面窗口句柄?
- 20. 获取C子窗口句柄#
- 21. 从GtkWidget获取X11窗口句柄
- 22. 从Selenium webdriver的当前窗口获取窗口句柄(IntPtr)GUID
- 23. 获取窗口在光标下的窗口句柄
- 24. C# - 获取活动窗口下的窗口句柄?
- 25. 获取上次激活窗口的窗口句柄
- 26. 获取VB.NET中PID进程的所有子窗口的句柄
- 27. 从窗口类名称获取窗口句柄
- 28. 从窗口句柄获取窗口实例
- 29. 从窗口句柄获取窗口z索引
- 30. 获取应用程序的主窗口
看看这个工作的解决方案:http://stackoverflow.com/a/28055461/1274092 – 2015-01-20 21:30:01