查找Firefox子窗口(警报)

时间:2015-12-01 11:49:25

标签: c# winforms pinvoke

我试图根据其父级(在本例中为Firefox)找到一个javascript警报窗口。

如果我全局获取警报并检查其所有者,我会得到firefox。为什么我不能反过来找到火狐的孩子的警报?

IntPtr firefoxHwnd = GetFirefoxHwnd(); // this works

WindowHandleInfo whi = new WindowHandleInfo(firefoxHwnd);
List<IntPtr> children = whi.GetAllChildHandles(); // 0 items

IntPtr alertHwnd = Pinvoke.FindWindow("MozillaDialogClass", null);
IntPtr alertOwnerHwnd = Pinvoke.GetWindow(alertHwnd, GW_OWNER);

Debug.WriteLine(firefoxHwnd == alertOwnerHwnd); // true

http://blog.ralch.com/2015/04/pinvoke-getting-all-child-handles-of-window/

1 个答案:

答案 0 :(得分:0)

由于我在一个单独的线程中创建Firefox,这似乎可以解决问题:

delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);

[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn,IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]

static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);

public static IEnumerable<IntPtr> EnumerateProcessWindowHandles(IntPtr hwnd)
{
    List<IntPtr> handles = new List<IntPtr>();
    uint processId;
    GetWindowThreadProcessId(hwnd, out processId);

    foreach (ProcessThread thread in Process.GetProcessById((int) processId).Threads)
    {
        EnumThreadWindows(thread.Id, (hWnd, lParam) => 
                                    { handles.Add(hWnd); return true; }, IntPtr.Zero);
    }

    return handles;
}