我正在構建一個應用程序,它給出了另一個應用程序的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