wry icon indicating copy to clipboard operation
wry copied to clipboard

Ability to access the underlaying HWND

Open tnthung opened this issue 10 months ago • 3 comments

Is your feature request related to a problem? Please describe. Although the current provided API has a wide coverage to more common use cases, some low-level operations via Win32 API are still mandatory from time to time. In this case, the ability to get the actual HWND for the webview would be necessary.

Describe the solution you'd like Add the hwnd() or any variant to the WebView type to get the hwnd inside the InnerWebView.

Describe alternatives you've considered Current there's no other way to get the HWND as far as I can tell. Since the webview does not have a title, the FindWindowW function cannot be used to retrieve the handle for webview window. Also, the hwnd itself has been hidden behind the InnerWebView and WebView is only served as a proxy to work with the inner one.

tnthung avatar Jan 27 '25 11:01 tnthung

The WebView2 library makes it intentionally hard to get the hwnd, i think this is this bs we do to get it: https://github.com/tauri-apps/wry/blob/9ff1b66b52d14681711403d8c7d41347c46dc741/src/webview2/drag_drop.rs#L51-L61

The hwnd you see in the InnerWebView is the hwnd of the parent window that you provide yourself.

FabianLars avatar Jan 27 '25 13:01 FabianLars

Sorry for the late reply. I've tried this method after I opened the issue, but more than one HWND was reported (4 in my case, The parent HWND is the winit window created by bevy). How do I ensure which one is the one for the webview and manipulate it without accidentally tweaking others?

tnthung avatar Feb 01 '25 01:02 tnthung

I've managed to do this in a somewhat quirky way.

  1. Build the webview by passing the bevy's HWND
  2. Use EnumChildWindows to iterate through the hierarchy.
  3. Use GetWindowLongPtrW(hwnd, GWLP_WNDPROC) in the function passed into step 2.
  4. The first HWND returned non-zero value seems to be the one.

There're actually 2 windows returning non-zero, I'm just assuming the first one is the one by using subclass and print out the message it received.

tnthung avatar Feb 04 '25 09:02 tnthung