Setting a callback for tray icon message handling

2019-06-03 14:50发布

I was used to using WndProc for handling proc messages, but now I have a console project and I want somehow to handle a custom WM_TRAYICON message.

I have the callback function, I just need to make Windows call it whenever someone interacts with the tray icon. In a recent program I did I used SetWindowsHookEx, can something similar be used?

2条回答
狗以群分
2楼-- · 2019-06-03 15:18

You need to create a hidden window to use with the notification icon. Call CreateWindow as usual to create a window, but just refrain from showing it. Make sure that you do not pass WS_VISIBLE when creating the window.

The other issue is that you need a message loop. Since a console application will not run a message loop by default, the simplest solution is to run the notification icon out of a separate thread which implements the message loop. Naturally this implies that the hidden window needs to be created and destroyed in that thread also.

查看更多
Ridiculous、
3楼-- · 2019-06-03 15:28

I propose you do not make your project console. If you want to handle window messages in legitimate way (without hooks and hacks) and you want to have a console then typical solution is to use AllocConsole function.

查看更多
登录 后发表回答