简体   繁体   English

C ++创建程序在后台运行

[英]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. 另外,在下面的代码中, hWndHWND ,这是您要与通知图标关联的窗口。 This HWND's wndProc will receive the messages for the icon. HWND's wndProc将接收该图标的消息。

Notes: 笔记:

  • the NIF_ICON flag makes the hIcon valid in the NOTIFICATIONICONDATA structure. NIF_ICON标志使hIconNOTIFICATIONICONDATA结构中有效。 So if you don't want to have an icon, don't specify it. 因此,如果您不想有图标,请不要指定它。
  • the NIF_MESSAGE flag makes the uCallbackMessage valid. NIF_MESSAGE标志使uCallbackMessage有效。 If you don't want to handle any messages, don't specify this flag. 如果您不想处理任何消息,请不要指定此标志。
  • You have to remove the icon before you shut down your program, or it will get stuck there until you hover over it 在关闭程序之前,必须先删除该图标,否则它将停留在该图标上,直到将鼠标悬停在该图标上为止
  • At startup of your computer, Shell_NotifyIcon may have some difficulties to succeed. 在计算机启动时,Shell_NotifyIcon可能会有些困难才能成功。 I can't find the reference for it, but I know I have read it somewhere.. So, when not successful, don't assume that it will not work at all - just try again. 我找不到它的参考,但是我知道我已经在某处阅读了它。.因此,如果没有成功,请不要以为它根本不起作用-请再试一次。

With this said, this is how you add, remove and handle the messages for the tray icon 如此说来,这就是您添加,删除和处理任务栏图标消息的方式

To add the 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);

To remove the icon 删除图标

NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = /*TrayIcon::*/ID;
Shell_NotifyIcon(NIM_DELETE, &nid);

Handling the messages for the icon 处理图标的消息

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. 您将需要一个窗口和一个消息循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM