I am making a window-switcherin tauri using Rust and solidjs.
I am using winapi to interact with Windows.
For now I have:
- HashMap of <window_title (string), HWND >
- I send the window titles to the frontend
- When clicked, it correctly switches the windows.
I want to display the app icon beside its title, so it can be more recognizable.
This is my method:
unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: LPARAM) -> BOOL {
let title_length = GetWindowTextLengthW(hwnd);
if title_length > 0 {
let mut buffer: Vec<u16> = vec![0; (title_length + 1) as usize]; // +1 for null terminator
GetWindowTextW(hwnd, buffer.as_mut_ptr(), buffer.len() as i32);
if IsWindowVisible(hwnd) != 0 {
let window_title = OsString::from_wide(&buffer).to_string_lossy().to_string();
let title_map = &mut *(lparam as *mut HashMap<String, (HWND, HICON)>);
let app_icon = get_app_icon(hwnd);
if app_icon.is_some() {
title_map.insert(window_title, (hwnd, app_icon.unwrap()));
}
}
}
1
}
fn get_app_icon(hwnd: HWND) -> Option<HICON> {
let icon_handle: usize = unsafe { GetClassLongPtrW(hwnd, GCLP_HICON as i32) };
if icon_handle == 0 {
None
} else {
Some(icon_handle as HICON)
}
}
I modified it to have the icon beside the window handle
Is my method for retrieving the HICON correct? How can I log it? I tried to print the icon_handle
but it shows nothing. But when I try to log any string in its place it logs correctly.
2
Answers
Thanks @DeveloperMindset.com for their crucial help.
I modified it a bit to make it work with winapi since i wasnt able to make it work with window.rs, and change it in a way that extracts the icon from the exe path.
Here’s how you can convert
HICON
to abase64
representation ofPNG
icon:=============
So Tauri is built on top of TAO, which is a fork of winit and they implement window management for each platform there.
Specifically windows/window.rs, so in your case I would clone the TAO repository and add required changes there, and then patch your dependencies to make it point to your version of TAO.
Now, here’s how TAO implements
hicon
related class:Oh, it’s worth mentioning, Tauri uses windows crate, instead of
winapi
. Which has less downloads, but IMO better support from Microsoft. And essentially very similar to what apple-sys done for Macs.As Jonathan Potter pointed out, you need to use GetIconInfo to get hicon info:
So to answer your original question, no you can’t print
hicon
because it’s just a handle:P.S. This is how TAO check visibility of a window on Windows.
P.P.S. Same place has ~7 ways to get your windows hardware id in
HWND
and other formats.