[英]How to get Edge window handle (HWND)?
我使用 CreateCoreWebView2Host() 方法創建了邊緣瀏覽器 window。 此方法采用父 window 句柄並創建子 window ,我們可以在其中導航 web 頁面。 完成導航后,我需要返回我的 window 句柄,我相信我無法返回。
在 Spy++ 上,我看到“Chrome_WidgetWin_0”、“Chrome_WidgetWin_1”、“中間 D3D 窗口”作為孩子 windows 到我的父母 window。 哪個是子 window 手柄?我以為我正在創建一個子 window。
我嘗試使用傳遞上述 class 名稱的 FindWindowEx() 獲取 window 句柄。 但在我的項目中仍然沒有得到預期的結果。 所以我懷疑我是否傳遞了正確的句柄。
現在的問題是,如何獲取 CreateCoreWebView2Host 創建的 window 的 window 句柄(HWND)?
您可以使用GetWindow
獲取邊緣 window 句柄,將WebView2
控件的Handle
作為第一個參數傳遞給它,將GW_CHILD
作為第二個參數傳遞給它。 例如:
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);
}
WebView2 SDK 不提供此 HWND,因為 WebView2 連接到提供的父 HWND 的確切方式旨在成為可能會隨着底層 Edge 瀏覽器或 WebView2 運行時更新而更改的實現細節,即使您使用相同版本的WebView2 SDK。 我們在很大程度上依賴於瀏覽器的渲染邏輯,所以這在未來可能會改變。
CoreWebView2Host(在最近的 SDK 版本中重命名為CoreWebView2Controller )不提供 HWND,而是為您提供了各種聚焦、縮放等方法。 你想用 HWND 做什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.