Thread: How to minimize to the systray and not to the taskbar?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    How to minimize to the systray and not to the taskbar?

    Any idea?

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Here's the code for a small backup utility that I wrote at work.
    You're free to use it however you like. Unfortunately since it was
    such a small app I wrote in a hurry, I didn't comment it <-I know,
    I know, no excuse :) but the billing department needed a quick
    fix, I was on a time crunch *honest* :)

    I've added a few comments here to signify the unique
    code for the system tray functionality. But if you have any
    questions feel free to ask and when I get more time (I'm at work
    now) I'll check back and answer them.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <mmsystem.h>
    #include <shellapi.h>  //<-this is needed for a systray app
    #include <string>
    using namespace std;
    
    #define ID_TASKBARICON 100  //<-systray app
    #define WM_ICONNOTIFY (WM_USER + 101) //<-systray app
    
    HWND hWnd=NULL;
    HINSTANCE hInst=NULL;
    HANDLE hFile=NULL;
    string sFiles[4], sLocalPath, sLocalFile, sServerPath, sServerFile;
    bool bBackup;
    FILETIME ftC, ftA, ftW; //created, accessed and written file times
    SYSTEMTIME stCur, stFile; //current and file system times
    DWORD dwLast;
    int i;
    NOTIFYICONDATA nid; //<-systray app
    
    HWND hBtnMin=NULL, hBtnBkup=NULL, hBtnExit=NULL;
    
    void InitVars();
    void MidnightRollover();
    bool BackupFiles();
    void MainLoop();
    
    LRESULT CALLBACK MsgHndlr(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
    {
    	switch(umsg)
    	{
    	case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			return(0);
    		}break;	
    	case WM_ICONNOTIFY: //<-systray app msg
    		{
    			switch(lparam)
    			{
    			case WM_LBUTTONDBLCLK:
    				{
    					ShowWindow(hWnd,SW_RESTORE);
    				}break;
    			}
    			return(0);
    		}break;	
    	case WM_LBUTTONDOWN:
    		{
    			BackupFiles();
    			bBackup=false;
    			return(0);
    		}break;
    	case WM_COMMAND:
    		{
    			if((HWND)lparam==hBtnMin)
    			{
    				ShowWindow(hWnd,SW_HIDE);
    				return(0);
    			}
    			if((HWND)lparam==hBtnExit)
    			{
    				DestroyWindow(hWnd);
    				return(0);
    			}
    		}break;
    	case WM_CREATE:
    		{
    			hBtnMin=CreateWindowEx(0,"Button","Minimize",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,150,5,100,30,hwnd,NULL,hInst,NULL);
    			hBtnExit=CreateWindowEx(0,"Button","Exit",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,260,5,50,30,hwnd,NULL,hInst,NULL);
    			return(0);
    		}break;
    	}
    	return(DefWindowProc(hwnd,umsg,wparam,lparam));
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	hInst=hInstance;
    	WNDCLASSEX wcx;
    	wcx.cbSize=sizeof(WNDCLASSEX);
    	wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    	wcx.lpfnWndProc=MsgHndlr;
    	wcx.cbClsExtra=0;
    	wcx.cbWndExtra=0;
    	wcx.hInstance=hInst;
    	wcx.hIcon=LoadIcon(NULL, IDI_APPLICATION);
    	wcx.hCursor=LoadCursor(NULL, IDC_ARROW);
    	wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    	wcx.lpszMenuName=NULL;
    	wcx.lpszClassName="WindowClass";
    	wcx.hIconSm=NULL;
    	if(!RegisterClassEx(&wcx)) return(0);
    	hWnd=CreateWindowEx(0,"WindowClass","Tidewater Billing Backup",WS_CAPTION,
    		GetSystemMetrics(SM_CXSCREEN)/2-210,GetSystemMetrics(SM_CYSCREEN)/2-40,420,80,
    		NULL,NULL,hInst,NULL);
    	if(!hWnd) return(0);
    	InitVars();
    	MSG msg;
    	for(;;)
    	{
    		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    		{
    			if(msg.message==WM_QUIT) break;
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    		MainLoop();
    	}
    	Shell_NotifyIcon(NIM_DELETE,&nid); //<-systray app
    	return(msg.wParam);
    }
    
    void InitVars()
    {
    	sLocalPath="C:\\IMATEBKUP\\";
    	sServerPath="V:\\IMATEBKUP\\";
    	sFiles[0]="IMATEBKUP1100";
    	sFiles[1]="IMATEBKUP1200";
    	sFiles[2]="IMATEBKUP3100";
    	sFiles[3]="IMATEBKUP3800";
    	dwLast=0;
    	bBackup=false;
    	GetLocalTime(&stFile);	
    
                    //systray app ->
    	nid.cbSize=sizeof(NOTIFYICONDATA);
    	nid.hWnd=hWnd;
    	nid.uID=ID_TASKBARICON;
    	nid.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
    	nid.uCallbackMessage=WM_ICONNOTIFY;
    	nid.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    	strcpy(nid.szTip,"tooltip");
    	Shell_NotifyIcon(NIM_ADD,&nid);
                    //<- systray app
    }
    
    void MidnightRollover()
    {
    	GetLocalTime(&stCur);
    	if((stCur.wHour==0)&&(stCur.wMinute>=30))
    	{
    		bBackup=false;
    	}
    }
    
    void MainLoop()
    {
    	if((timeGetTime()-dwLast)>=900000)
    	{
    		dwLast=timeGetTime();
    		MidnightRollover();
    		if(!bBackup)			
    		{
    			GetLocalTime(&stCur);
    			if((stCur.wHour==20)&&(stCur.wMinute>=5))
    			{
    				if(BackupFiles()) bBackup=true;				
    			}
    		}					
    	}
    }
    
    bool BackupFiles()
    {	
    	for(i=0;i<4;i++)
    	{
    		sLocalFile=sLocalPath;
    		sLocalFile+=sFiles[i];
    		sServerFile=sServerPath;
    		sServerFile+=sFiles[i];
    		CopyFile(sLocalFile.c_str(),sServerFile.c_str(),0);
    	}
    	return(true);
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    Very cool I have just implemented that on my code and the Icon shows on my tray every time i open my app, and when i hit the minimize button the app disappears.

    The problems right now are:

    1)It disappears forever When i pass my mouse over the tray icon (no need to click it) the icon disappears.

    2)I can´t find my app using alt+tab and she´s not at the taskbar either hehehe. So the only solution is ctrl+alt+del :\


    But thanks a lot for the help, i´m sure eventually i´ll find a way.
    If you have any idea what that could be, i´d be happy to hear!

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    Problem solved, it was stupid syntax stuff

Popular pages Recent additions subscribe to a feed