简体   繁体   English

如何获得 Edge window 手柄(HWND)?

[英]How to get Edge window handle (HWND)?

I created edge browser window using CreateCoreWebView2Host() method.我使用 CreateCoreWebView2Host() 方法创建了边缘浏览器 window。 This method is takes parent window handle and creates child window in which we can navigate the web page.此方法采用父 window 句柄并创建子 window ,我们可以在其中导航 web 页面。 After I am done with navigation I need to return my window handle, which I believe I am failed to return.完成导航后,我需要返回我的 window 句柄,我相信我无法返回。

On Spy++ I see "Chrome_WidgetWin_0", "Chrome_WidgetWin_1", "Intermediate D3D Window" as child windows to my parent window.在 Spy++ 上,我看到“Chrome_WidgetWin_0”、“Chrome_WidgetWin_1”、“中间 D3D 窗口”作为孩子 windows 到我的父母 window。 which one is the child window handle?I thought I am creating one child window.哪个是子 window 手柄?我以为我正在创建一个子 window。

I tried by fetching window handles using FindWindowEx() passing above mentioned class names.我尝试使用传递上述 class 名称的 FindWindowEx() 获取 window 句柄。 But still not getting expected results in my project.但在我的项目中仍然没有得到预期的结果。 So I doubt if I am passing correct handle.所以我怀疑我是否传递了正确的句柄。

Now the question is, How to get the window handle(HWND) for the window created by CreateCoreWebView2Host?现在的问题是,如何获取 CreateCoreWebView2Host 创建的 window 的 window 句柄(HWND)?

You can get the edge window handle by using GetWindow , passing Handle of WebView2 control to it as the first argument and GW_CHILD as second argument.您可以使用GetWindow获取边缘 window 句柄,将WebView2控件的Handle作为第一个参数传递给它,将GW_CHILD作为第二个参数传递给它。 For exaample:例如:

public const uint GW_CHILD = 5;
[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll")]
public static extern IntPtr SetFocus(IntPtr hWnd);

WebView2 webView21 = new Microsoft.Web.WebView2.WinForms.WebView2();
private async void Form1_Load(object sender, EventArgs e)
{
    webView21.Dock = DockStyle.Fill;
    this.Controls.Add(webView21);
    await webView21.EnsureCoreWebView2Async();
    webView21.Source = new Uri("https://bing.com");

    webView21.NavigationCompleted += WebView21_NavigationCompleted;
}

private void WebView21_NavigationCompleted(
    object sender, CoreWebView2NavigationCompletedEventArgs e)
{
    var child = GetWindow(webView21.Handle, GW_CHILD);
    SetFocus(child);
}

Note: You may find this GitHub thread useful.注意:您可能会发现此GitHub 线程很有用。 This is the trick that I've used to focus the browser in Windows Forms as well.这也是我用来将浏览器聚焦在 Windows Forms 中的技巧。

The WebView2 SDK doesn't provide this HWND because exactly how the WebView2 connects up to the provided parent HWND is intended to be an implementation detail that could change as the underlying Edge browser or WebView2 Runtime is updated even when you stay on the same version of the WebView2 SDK. WebView2 SDK 不提供此 HWND,因为 WebView2 连接到提供的父 HWND 的确切方式旨在成为可能会随着底层 Edge 浏览器或 WebView2 运行时更新而更改的实现细节,即使您使用相同版本的WebView2 SDK。 We're relying in large part on the browser's logic for rendering and so this may change in the future.我们在很大程度上依赖于浏览器的渲染逻辑,所以这在未来可能会改变。

Instead of providing an HWND, the CoreWebView2Host (its been renamed to CoreWebView2Controller in more recent SDK releases) provides various methods for you to focus, zoom, and so on. CoreWebView2Host(在最近的 SDK 版本中重命名为CoreWebView2Controller )不提供 HWND,而是为您提供了各种聚焦、缩放等方法。 What are you trying to do with the HWND?你想用 HWND 做什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM