1

So I have a program which doesn't have a console. It starts up by making a dummy HWND which it then hides and then acts as a notification area application (stays in tray). People can hover over the program to view its status and press hotkeys to perform tasks on screen.

Anyway, I want to display my programs icon in the tray but cannot. I added the icon resource to Visual studio and my executable has an icon in explorer. The resource has sizes from 16x16 - 256x256.

Now, I set NOTIFYICONDATA's info to: nid.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1));

and included "resource.h"

Which defines IDI_ICON1 as 104. However when I use this the blue explanation point icon shows up (ie one of the windows default ones). I tried IDI_ERROR and the error icon showed up fine. I tried a custom 12x12 icon as I read size may be the issue but that didnt work either.

IDI_APPLICATION uses the default icon for the application, ie the default .exe icon.

I currently don't know what to try or do.

Thanks in advance for your help!

Mo Beigi
  • 1,614
  • 4
  • 29
  • 50
  • 1
    *Never* skip error checking when you use the raw winapi. At a minimum use an assert() to verify your assumptions. You'll now see that LoadIcon() fails. – Hans Passant Feb 23 '14 at 15:16
  • Yes you are right. I am unfamiliar with most of this taskbar related API so I have committed some checks. – Mo Beigi Feb 23 '14 at 16:56

1 Answers1

4

An MSDN article for LoadIcon() says this about its first argument:

A handle to an instance of the module whose executable file contains the icon to be loaded. This parameter must be NULL when a standard icon is being loaded.

Since you're not loading the standard icon, but your own, you need to provide a valid module handle. The module handle for your executable could be retrieved with passing NULL to GetModuleHandle() function, so the code would look like this:

nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
mike.dld
  • 2,929
  • 20
  • 21
  • Yes you are correct. So I made the wrong assumption that NULL would be a NULL handle, I needed to use GetModuleHandle(NULL). Thank you! – Mo Beigi Feb 23 '14 at 16:57