如何在控件中找到句柄?

时间:2022-09-02 13:33:29

There's a DialogBox raised by the WebBrowser inside my application that I need to find. I tried this:

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

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

but it does return IntPtr.Zero

但它确实返回IntPtr.Zero

it does works fine:

它确实工作正常:

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

But I want to search for the windows inside webBrowserEx1 control only and not a global search like does FindWindow()

但我想只搜索webBrowserEx1控件中的窗口,而不是像FindWindow()那样搜索全局搜索

Update: Using spy++ I can see that the DialogBox that the neither the DialogBox's first child window nor the owner is the WebBrowser (I guess that's why it doesn't works) but the parent window is my own application (where the WebBrowser is hosted on) so I updated my code like this:

更新:使用spy ++我可以看到,DialogBox的第一个子窗口和所有者都不是WebBrowser的DialogBox(我猜这就是为什么它不起作用)但是父窗口是我自己的应用程序(WebBrowser托管在哪里) )所以我更新了我的代码:

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

but it didn't worked either.

但它也没有用。

1 个解决方案

#1


It could be the Dialog is not a direct child of the WebBrowser - maybe you can verify that with Spy++.

可能是Dialog不是WebBrowser的直接孩子 - 也许你可以用Spy ++验证它。

And just by coincidence just yesterday I stumbled across a piece of c# code I used years ago for recursively searching through child windows. Maybe that helps:

就在昨天巧合的时候,我偶然发现了一些我多年前用于递归搜索子窗口的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;
}

#1


It could be the Dialog is not a direct child of the WebBrowser - maybe you can verify that with Spy++.

可能是Dialog不是WebBrowser的直接孩子 - 也许你可以用Spy ++验证它。

And just by coincidence just yesterday I stumbled across a piece of c# code I used years ago for recursively searching through child windows. Maybe that helps:

就在昨天巧合的时候,我偶然发现了一些我多年前用于递归搜索子窗口的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;
}