Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to access the underlaying HWND #1471

Open
tnthung opened this issue Jan 27, 2025 · 1 comment
Open

Ability to access the underlaying HWND #1471

tnthung opened this issue Jan 27, 2025 · 1 comment

Comments

@tnthung
Copy link

tnthung commented Jan 27, 2025

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.

@FabianLars
Copy link
Member

The WebView2 library makes it intentionally hard to get the hwnd, i think this is this bs we do to get it:

{
let mut callback = |hwnd| controller.inject_in_hwnd(hwnd, handler.clone());
let mut trait_obj: &mut dyn FnMut(HWND) -> bool = &mut callback;
let closure_pointer_pointer: *mut c_void = unsafe { std::mem::transmute(&mut trait_obj) };
let lparam = LPARAM(closure_pointer_pointer as _);
unsafe extern "system" fn enumerate_callback(hwnd: HWND, lparam: LPARAM) -> BOOL {
let closure = &mut *(lparam.0 as *mut c_void as *mut &mut dyn FnMut(HWND) -> bool);
closure(hwnd).into()
}
let _ = unsafe { EnumChildWindows(Some(hwnd), Some(enumerate_callback), lparam) };
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants