Question

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. And now i need to find a way to send it into the tray.

type: MFC (Dialog-Window) lnguage: 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. thanks.

EDIT 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 ...)

EDIT 2:

this->ShowWindow(SW_HIDE);

also doesnt work. same problem.

Was it helpful?

Solution 2

You are looking for the NotifyIcon class: 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/

That said just adding your application to the system tray is a really bad UI idea. 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top