4

I have a program that literally consists of a tray icon. No GUI is needed. However, when writing the win32 code, is it necessary to still initialize a hWnd object to be associated with the tray icon?

For instance, it is normal to have a the NOTIFYICONDATA hWnd field point to the window's handle. Like

nid.hWnd = hwnd;

Essentially, will my icon be able to still receive messages if i set

nid.hwnd = NULL;
Steve Barna
  • 1,378
  • 3
  • 13
  • 23

2 Answers2

7

How would you receive messages without a window?

Yes you need a window associated with the tray icon.

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • So I must create a window, associate it and hide it? – Steve Barna Sep 28 '11 at 23:41
  • Create the window without `WS_VISIBLE`, and it won't be visible to begin with (no "hiding" necessary). Message-only windows are very common. – tenfour Sep 28 '11 at 23:43
  • 1
    You can also have [message-only Windows](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632599%28v=vs.85%29.aspx#message_only), but I'm not sure if they work correctly with notify icons. – Matteo Italia Sep 28 '11 at 23:47
  • 1
    Well it appears Message only windows do not "receive broadcast messages". For instance, if the task bar crashes and resets, I don't think I would get the `wm_taskbarcreated` message. @MatteoItalia – Steve Barna Sep 29 '11 at 00:42
  • Yea I should not have used that wording. I just meant a non-visible window meant only for receiving messages. – tenfour Sep 29 '11 at 00:43
3

You could create a message-only window by specifying HWND_MESSAGE creating the window. However, message-only windows do not receive broadcast messages and you would miss out on the TaskbarCreated message. This message tells your application that explorer.exe has restarted and that your application needs to re-add its notification icons. Rather important. So create a window that never becomes visible: never call ShowWindow().

Kay Zed
  • 1,304
  • 2
  • 21
  • 31