Get handles of all windows in a WPF process

I know I can get the MainWindowHandle by enumerating Process.GetProcesses but I also want to get handles of all WPF windows a WPF app may have. I need to do this in a completely separate process. How would I do this? Thanks


Since a WPF process has multiple thread you have to enumerate all threads to find the windows.

I used the below Win32 method to do this. You create a list and add to the list in the callback that is called on each iteration.

[DllImport("user32.dll")] 
public static extern bool EnumThreadWindows(int dwThreadId, EnumThreadWindowsCallBack lpfn, IntPtr lParam);

 Win32.EnumThreadWindows(processThread.Id, (hWnd, lParam) =>
                {
                    if (Win32.IsWindowVisible(hWnd))
                    {
                        Win32.RECT windowRect;
                        Win32.GetWindowRect(hWnd, out windowRect);

                        if (hWnd != _currentWindowHandle)
                        {
                            windowList.Add(new WindowInfo(hWnd, windowRect));
                        }
                    }
                    return true;
                }, IntPtr.Zero);
链接地址: http://www.djcxy.com/p/38846.html

上一篇: Visual Studio添加任何引用或安装和更新任何包(加载类型库/ DLL)时出错

下一篇: 获取WPF过程中所有窗口的句柄