0

I am trying to create a simple tray icon using the windows-rs rust crate.

When calling Shell_NotifyIconA(NIM_ADD, &data); one is created, however, by default it has a blank image. I have loaded an .ico via LoadImageW, however, still nothing.

What am I missing?

Full code:

use std::ffi::OsString;

use windows::Win32::Foundation::{HINSTANCE, GetLastError};
use windows::Win32::UI::Shell::{Shell_NotifyIconA, NOTIFY_ICON_MESSAGE, NIM_ADD, NOTIFYICON_VERSION_4, NOTIFYICONDATAA};
use windows::Win32::UI::WindowsAndMessaging::{LoadImageW, IMAGE_ICON, LR_DEFAULTSIZE, LR_LOADFROMFILE, DestroyIcon, HICON};

fn main() -> anyhow::Result<()> {
    unsafe {
        let mut data = NOTIFYICONDATAA::default();
        let icon_handle = LoadImageW(HINSTANCE::default(), &OsString::from("icon.ico").into(), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE)?;

        println!("icon handle {:#?}", icon_handle); // icon handle 363530555
        data.hIcon = HICON(icon_handle.0);
        println!("error? {:#?}", GetLastError()); // 0

        Shell_NotifyIconA(NIM_ADD, &data);
    }
    
    Ok(())
}
Gremious
  • 304
  • 3
  • 10
  • 2
    You need to specify which fields are valid using the flags: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-notifyicondataa – Luke Sep 22 '22 at 21:42
  • @Luke Yep, I missed that, thank you! Do consider writing your comment as an answer reply so that I can mark it as the solution :) – Gremious Sep 24 '22 at 13:05

0 Answers0