Reputation: 7855
I'm trying to make a window I created using the winit
crate be always on top (HWND_TOPMOST
). I'm creating my window, and getting the RawWindowHandle::Windows
from it. That struct has a pub hwnd
which is a *mut c_void
. And my question now is, how can I convert that *mut c_void
to a *mut winapi::shared::windef::HWND__
so I can pass it to winapi::winuser::SetWindowPos(...)
?
Here's my code for getting the raw window handle:
let win_handle = match window.raw_window_handle() {
RawWindowHandle::Windows(windows_handle) => windows_handle.hwnd,
_ => panic!("Unsupported platform!"),
};
And this is my code passing win_handle
to SetWindowPos
:
unsafe {
if winuser::SetWindowPos(win_handle, winuser::HWND_TOPMOST, 0, 0, 0, 0, winuser::SWP_NOMOVE | winuser::SWP_NOSIZE) == 1 {
println!("Success");
} else {
println!("Failure");
}
}
What am I doing wrong?
Upvotes: 0
Views: 947
Reputation: 7855
Turns out, there is a HWND struct to which you can simply cast the *mut c_void
, like so:
let winapi_handle = win_handle as winuser::shared::windef::HWND;
// Or even simple, cast it in the call with 'as _'
// Shoutout to @IInspectable
SetWindowPos(win_handle as _, winuser::HWND_TOPMOST, 0, 0, 0, 0, winuser::SWP_NOMOVE | winuser::SWP_NOSIZE)
Edit: It was only after I tried this that I actually googled "Winit set window always on top" and found this. So, when constructing your window using a WindowBuilder
you can simply call
window_builder.with_always_on_top(true)
And it will do it for you on all supported platforms (Linux, Windows and MacOS. Adnroid, iOS and the Web do not support it)
Upvotes: 1