问题描述:

My goal is to have a single icon for all the windows of my application.

After some reading, my understanding is that creating a tray icon is achieved through Shell_NotifyIcon(). This function gets a NOTIFYICONDATA structure which contains a hWnd field. This HWND is used by the system to notify the corresponding window of tray icon events. These events are handled by a WindowProc callback that is set on the window with SetWindowLongPtr().

Hence my questions:

  • How can a single icon notify all the windows of my app of say a left mouse click ?
  • Can I Shell_NotifyIcon() multiple times with different NOTIFYICONDATA structures, each one with a different hWnd, but with the same icon ?
  • What if the original window that got registered for creating the tray icon is destroyed ?

Would creating a hidden proxy window be an appropriate solution ?

Some background: my application calls the WinAPI with C (using js-ctypes), and should ideally work on all Windows versions from XP on.

网友答案:

You practically answered your own question in the question itself. The best thing to do is create a hidden window that survives as long as you need the tray icon to exist.

You would call Shell_NotifyIcon() only once with the hWnd referring to this hidden window, and have this window post the messages to the individual windows that need to receive them.

This also gives you the flexibility of being able to decide to skip sending messages to a particular window, or being able to send a different message to each window, depending on the requirements of your particular application.

相关阅读:
Top