C# 如何在控件中找到句柄?

C# 如何在控件中找到句柄?,c#,winforms,winapi,webbrowser-control,findwindow,C#,Winforms,Winapi,Webbrowser Control,Findwindow,在我的应用程序中有一个由WebBrowser引发的对话框,我需要找到它。我试过这个: FindWindowEx(webBrowserEx1.Handle, IntPtr.Zero, "#32770", "title here") 但它确实返回IntPtr.Zero 它确实工作得很好: FindWindow("#32770", "title here") 但是我只想在webBrowserEx1控件中搜索窗口,而不是像FindWindow()这样的全局搜索 更新:使用spy++我可以看到对话框的

在我的应用程序中有一个由
WebBrowser
引发的对话框,我需要找到它。我试过这个:

FindWindowEx(webBrowserEx1.Handle, IntPtr.Zero, "#32770", "title here")
但它确实返回
IntPtr.Zero

它确实工作得很好:

FindWindow("#32770", "title here")
但是我只想在
webBrowserEx1
控件中搜索窗口,而不是像
FindWindow()这样的全局搜索

更新:使用spy++我可以看到对话框的第一个子窗口和所有者都不是
WebBrowser
(我想这就是它不工作的原因),但是父窗口是我自己的应用程序(WebBrowser托管在其中),所以我更新了我的代码如下:

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

但它也不起作用。

可能是该对话框不是
网络浏览器的直接子对象-也许您可以用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;
}
[DllImport(“user32.dll”,SetLastError=true)]
公共静态外部IntPtr FindWindowEx(IntPtr hwndParent、IntPtr hwndChildAfter、string lpszClass、string lpszWindow);
/// 
///使用FindWindowEx()递归搜索具有给定类和/或标题的子窗口。
/// 
公共静态IntPtr FindChildWindow(IntPtr hwndParent,字符串lpszClass,字符串lpszTitle)
{
返回FindChildWindow(hwndParent,IntPtr.Zero,lpszClass,lpszTitle);
}
/// 
///使用FindWindowEx()递归搜索具有给定类和/或标题的子窗口,
///在指定的子窗口之后启动。
///如果lpszClass为空,则它将匹配任何类名。它不区分大小写。
///如果lpszTitle为null,则它将匹配任何窗口标题。
/// 
公共静态IntPtr FindChildWindow(IntPtr hwndParent、IntPtr hwndChildAfter、字符串lpszClass、字符串lpszTitle)
{
//试着找一个匹配的。
IntPtr hwnd=FindWindowEx(hwndpresent,IntPtr.Zero,lpszClass,lpszTitle);
if(hwnd==IntPtr.Zero)
{
//在孩子们里面搜索。
IntPtr hwndChild=FindWindowEx(hwndpresent,IntPtr.Zero,null,null);
while(hwndChild!=IntPtr.Zero&&hwnd==IntPtr.Zero)
{
hwnd=FindChildWindow(hwndChild,IntPtr.Zero,lpszClass,lpszTitle);
if(hwnd==IntPtr.Zero)
{
//如果我们还没有找到,检查下一个孩子。
hwndChild=FindWindowEx(hwndpresent,hwndChild,null,null);
}
}
}
返回hwnd;
}
我现在正在使用(并正在学习使用)spy++。这是伟大的(我使用的是自动信息工具)。在Spy++的属性检查器的窗口选项卡上,它对两个窗口都显示“无”:第一个子窗口和所有者窗口。。。那个对话框并不是WebBrowser真正创建的,对吧?如果是,由谁来做?为什么所有者窗口甚至不是我的应用程序窗口(WebBrowser所在的位置)?