在控制台项目中设置托盘图标消息处理的回调 [英] Setting a callback for tray icon message handling in a console project
问题描述
我习惯使用WndProc处理proc消息,但现在我有一个控制台项目,我想以某种方式处理自定义 WM_TRAYICON
消息。
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.
我有回调函数,只要有人与托盘图标交互,我就需要让Windows调用它。在最近的程序中,我使用 SetWindowsHookEx
,可以使用类似的东西吗?
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?
推荐答案
您需要创建一个隐藏的窗口,用于通知图标。像往常一样调用 CreateWindow
创建一个窗口,但只是不显示它。确保在创建窗口时不会传递 WS_VISIBLE
。
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.
这篇关于在控制台项目中设置托盘图标消息处理的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!