2015-05-09 52 views
-1

在我的應用程序中需要找到WebBrowser引發的對話框。我嘗試這樣做:如何在控件內部找到句柄?

FindWindowEx(webBrowserEx1.Handle, IntPtr.Zero, "#32770", "title here") 

但它並返回IntPtr.Zero

它正常工作:

FindWindow("#32770", "title here") 

但我想搜索裏面webBrowserEx1控制,而不是像一個全球性的搜索窗口FindWindow()

更新:使用spy ++我可以看到DialogBox的DialogBox的第一個子窗口和所有者都不是WebBrowser(我猜這就是爲什麼它不起作用),但父窗口是我自己的應用程序(WebBrowser託管在其中),所以我更新了我的代碼這個:

handle = FindWindowEx(this.Handle, IntPtr.Zero, "#32770", "title here"); 

但它沒有奏效。

回答

1

它可能是對話框不是WebBrowser的直接孩子 - 也許你可以用Spy ++驗證。

而就在昨天巧合,我偶然發現了一些多年前用於遞歸搜索子窗口的c#代碼。也許這有助於:

[DllImport("user32.dll", SetLastError = true)] 
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 

/// <summary> 
/// Uses FindWindowEx() to recursively search for a child window with the given class and/or title. 
/// </summary> 
public static IntPtr FindChildWindow(IntPtr hwndParent, string lpszClass, string lpszTitle) 
{ 
return FindChildWindow(hwndParent, IntPtr.Zero, lpszClass, lpszTitle); 
} 

/// <summary> 
/// Uses FindWindowEx() to recursively search for a child window with the given class and/or title, 
/// starting after a specified child window. 
/// If lpszClass is null, it will match any class name. It's not case-sensitive. 
/// If lpszTitle is null, it will match any window title. 
/// </summary> 
public static IntPtr FindChildWindow(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszTitle) 
{ 
// Try to find a match. 
IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, lpszClass, lpszTitle); 
if (hwnd == IntPtr.Zero) 
{ 
    // Search inside the children. 
    IntPtr hwndChild = FindWindowEx(hwndParent, IntPtr.Zero, null, null); 
    while (hwndChild != IntPtr.Zero && hwnd == IntPtr.Zero) 
    { 
    hwnd = FindChildWindow(hwndChild, IntPtr.Zero, lpszClass, lpszTitle); 
    if (hwnd == IntPtr.Zero) 
    { 
    // If we didn't find it yet, check the next child. 
    hwndChild = FindWindowEx(hwndParent, hwndChild, null, null); 
    } 
    } 
} 
return hwnd; 
} 
+0

我現在正在使用(和學習使用)間諜。這很好(我正在使用AutoIt信息工具)。在Spy ++的Property Inspector中的Window選項卡它對兩者都表示「none」:第一個子窗口和所有者窗口...因此DialogBox並不是真正由WebBrowser創建的,對吧?如果是這樣,誰來做?爲什麼所有者窗口甚至不是我的應用程序的窗口(WebBrowser託管在哪裏)? – Jack

+0

請查看更新 – Jack