简体   繁体   English

C ++ MFC如何将应用程序发送到Tray

[英]C++ MFC how to send an application into Tray

i wrote an little application which does some operations in the background. 我写了一个小应用程序,它在后台执行一些操作。

because it doesnt needs attention of the user, i want to send the application into system tray, and when someone clicks on it, the window should popup again. 因为它不需要用户的注意,我想将应用程序发送到系统托盘,当有人点击它时,窗口应该再次弹出。

the problem is, I've wrote an MFC program (dialog) which does everything I want it to do. 问题是,我写了一个MFC程序(对话框),它可以完成我想要它做的所有事情。 And now i need to find a way to send it into the tray. 而现在我需要找到一种方法将其发送到托盘中。

type: MFC (Dialog-Window) lnguage: C++ IDE: Visual Studio 2012 类型:MFC(对话框窗口)语言:C ++ IDE:Visual Studio 2012

do you have any ideas of how i can do this? 你对我怎么做这个有什么想法吗? or do you maybe know a nice tutorial or the function/classes i need? 或者你可能知道一个很好的教程或我需要的功能/类?

i already searched at codeprojects.com and google but there seems not to be much about this. 我已经在codeprojects.com和google上搜索了但似乎并没有太多关于这一点。 thanks. 谢谢。

EDIT 1: 编辑1:

I tried something like this: 我试过这样的事情:

HWND hMainWindow = this->GetSafeHwnd();
HICON hProgramIcon = this->GetIcon(false);
char szTip[16] = "Tracker"; 
nidTrayIcon.cbSize = sizeof(nidTrayIcon); 
nidTrayIcon.hIcon = hProgramIcon; 
nidTrayIcon.hWnd = hMainWindow; 
nidTrayIcon.uCallbackMessage = (WM_USER + 1); 
nidTrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nidTrayIcon.uID = 0x0200; 
strcpy_s((char*)nidTrayIcon.szTip,16, szTip2);
nidTrayIcon.szTip[strlen(szTip)] = '\0'; 
Shell_NotifyIcon(NIM_ADD, &nidTrayIcon);

If i run this code, my application shuts down with code "0x03" (Path not found ...) 如果我运行此代码,我的应用程序关闭代码“0x03”(找不到路径...)

EDIT 2: 编辑2:

this->ShowWindow(SW_HIDE);

also doesnt work. 也不起作用。 same problem. 同样的问题。

You are looking for the NotifyIcon class: http://msdn.microsoft.com/en-gb/library/system.windows.forms.notifyicon.aspx 您正在寻找NotifyIcon类: http//msdn.microsoft.com/en-gb/library/system.windows.forms.notifyicon.aspx

and here is a tutorial: http://alanbondo.wordpress.com/2008/06/22/creating-a-system-tray-app-with-c/ 这是一个教程: http//alanbondo.wordpress.com/2008/06/22/creating-a-system-tray-app-with-c/

That said just adding your application to the system tray is a really bad UI idea. 这就是说只是将你的应用程序添加到系统托盘是一个非常糟糕的UI想法。 It used to be very popular during the windows 95/98 days and Microsoft helpfully provided a working code example that everyone copied and pasted. 它曾经在Windows 95/98期间非常受欢迎,微软帮助提供了一个每个人都复制和粘贴的工作代码示例。 Soon we had fish and eyes swimming in our system trays. 不久,我们的鱼和眼睛在我们的系统托盘中游动。 And the tray got fuller and fuller and hence completely useless. 并且托盘变得更饱满和更饱满,因此完全没用。

If your application is doing something that does not need user attention, make it a service, or just let the user minimize the dialog to the taskbar and show a progress bar on the dialog. 如果您的应用程序正在执行不需要用户注意的事情,请将其设为服务,或者让用户最小化对话框的对话框并在对话框上显示进度条。 That way hovering over the taskbar icon will allow the user to check progress. 这样,将鼠标悬停在任务栏图标上将允许用户检查进度。

Use the system try to show helpful notifications. 使用系统尝试显示有用的通知。 Add an icon only if the icon will convey a system level activity that could effect the users current work (like network connectivity). 仅当图标将传达可能影响用户当前工作的系统级活动(如网络连接)时,才添加图标。 Otherwise, don't clutter my system tray. 否则,请勿弄乱我的系统托盘。 I don't want to take my eyes off the work that needs my attention (you said yours doesn't) to look at a bouncing bunny in my system tray. 我不想把我的视线从需要我注意的工作(你说你的没有)上看到我的系统托盘中的弹跳兔子。

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

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