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

Add a windows specific method for setting the window icons based on the embedded exe icon. #3932

Open
dtzxporter opened this issue Sep 27, 2024 · 0 comments
Labels
DS - windows S - enhancement Wouldn't this be the coolest?

Comments

@dtzxporter
Copy link
Contributor

Description

Using the following code you can get the icon for the running process, then call WM_SETICON to apply it to any window.
This prevents storing duplicate copies of the icon just to also reuse it for the window icon.

        let Ok(path) = std::env::current_exe() else {
            return 0;
        };

        let Ok(path) = U16CString::from_os_str(path.as_os_str()) else {
            return 0;
        };

        // SAFETY:
        // Path is checked to be a valid u16cstring above. The result is checked for errors where
        // 1 = not an icon or exe.
        // 0 = any other error.
        // While this obviously leaks the icon, the icon won't change for the lifetime of the program and can be used in more
        // than one window during the lifetime of the program.
        let icon = unsafe { ExtractIconW(GetModuleHandleW(std::ptr::null()), path.as_ptr(), 0) };

        if icon as usize <= 1 {
            return 0;
        }

Relevant platforms

Windows

@dtzxporter dtzxporter added the S - enhancement Wouldn't this be the coolest? label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DS - windows S - enhancement Wouldn't this be the coolest?
Development

No branches or pull requests

2 participants