[英]c++ create program runs in the background
i wanna make a program runs in the background and shows an icon in notification area of taskbar. 我想让程序在后台运行,并在任务栏的通知区域显示一个图标。 I'm using win32. 我正在使用win32。 What api should i use? 我应该使用什么API? Do you know any good tutorials? 你知道任何好的教程吗?
To make a program run in the background, you either add it as a service or make it "unavailable" to shutdown (for instance, hide the window for the program). 要使程序在后台运行,您可以将其添加为服务,或者使其“不可用”以关闭(例如,隐藏该程序的窗口)。 To add an icon in the toolbar you use winapi. 要在工具栏中添加图标,请使用winapi。 Call Shell_NotifyIcon and pass in a NOTIFYICONDATA structure 调用Shell_NotifyIcon并传入NOTIFYICONDATA结构
This should be defined somewhere 应该在某处定义
enum TrayIcon {
ID = 13, CALLBACKID = WM_APP+1
};
Also, in the below code the hWnd
is a HWND
, which is the window that you want to associate with the notification icon. 另外,在下面的代码中, hWnd
是HWND
,这是您要与通知图标关联的窗口。 This HWND's
wndProc
will receive the messages for the icon. 该HWND's
wndProc
将接收该图标的消息。
NIF_ICON
flag makes the hIcon
valid in the NOTIFICATIONICONDATA
structure. NIF_ICON
标志使hIcon
在NOTIFICATIONICONDATA
结构中有效。 So if you don't want to have an icon, don't specify it. 因此,如果您不想有图标,请不要指定它。 NIF_MESSAGE
flag makes the uCallbackMessage
valid. NIF_MESSAGE
标志使uCallbackMessage
有效。 If you don't want to handle any messages, don't specify this flag. 如果您不想处理任何消息,请不要指定此标志。 With this said, this is how you add, remove and handle the messages for the tray icon 如此说来,这就是您添加,删除和处理任务栏图标消息的方式
// in HICON hIcon: this is the icon you want as the image in the tray
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = ID;
nid.uFlags = NIF_ICON | NIF_MESSAGE;
nid.hIcon = hIcon;
nid.uCallbackMessage = /*TrayIcon::*/CALLBACKID;
Shell_NotifyIcon(NIM_ADD, &nid);
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = /*TrayIcon::*/ID;
Shell_NotifyIcon(NIM_DELETE, &nid);
LRESULT wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch (msg){
// ...
case /*TrayIcon::*/CALLBACKID:
{
// here, you handle the messages for your tray icon
}
break;
// ...
}
}
http://www.winprog.org/tutorial/ is good for learning winapi and basically how Windows apps work. http://www.winprog.org/tutorial/对于学习winapi以及Windows应用程序的工作原理非常有用。 For the tray icon, use Shell_NotifyIcon
. 对于任务栏图标,请使用Shell_NotifyIcon
。 You will need a window, and a message loop for this. 您将需要一个窗口和一个消息循环。
CSystemTray works well from coeproject . CSystemTray在coeproject中运行良好 。 It is a wrapper around Shell_NotifyIcon . 它是Shell_NotifyIcon的包装。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.