System Tray Icon Example for Lite-C (pure)


youll need to provide the icon.

ive been using this in pure mode and it works well.. havent tested in legacy yet.

anyway System_Tray.h could be either merged with the included header files(litec.h,windows.h, etc.) or seperate .. whatever, you decide


System_Tray.h

Code:
#define bool BOOL 


#define IMAGE_ICON          1
#define LR_SHARED           0x00008000
#define LR_LOADFROMFILE     0x00000010
#define LR_VGACOLOR         0x00000080

typedef long HICON;
typedef char CHAR;


typedef struct  _NOTIFYICONDATA{
    DWORD   cbSize;
    HWND    hWnd;
    UINT    uID;
    UINT    uFlags;
    UINT    uCallbackMessage;
    HICON   hIcon;
    CHAR    szTip[128];
}NOTIFYICONDATA;

typedef long PNOTIFYICONDATAA;




#define STDAPICALLTYPE     __stdcall

#define SHSTDAPI_(type)    EXT type STDAPICALLTYPE 

SHSTDAPI_(BOOL) Shell_NotifyIcon(DWORD dwMessage, PNOTIFYICONDATAA lpData);
 
#define ShellAPI(name,lib) #define PRAGMA_API name;lib!name

ShellAPI(Shell_NotifyIcon,shell32)




System_Tray.c


Code:
#include <acknex.h>
#include <windows.h>

#include "System_Tray.h"



NOTIFYICONDATA _nid;

HMENU hMenu, hSubMenu;

#define ID_MENU_EXIT          100
#define ID_MINIMIZE_TO_TRAY   101
#define ID_MENU_ITEM          102
#define ID_SUBMENU_ITEM       103
#define ID_RESTORE            104

bool minimized = false;
bool minimized_to_tray = false;



void _exit()
{
	
 /* delete the tray icon.*/
          	
   Shell_NotifyIcon(NIM_DELETE, &_nid);
  
   sys_exit(NULL); 
}



HWND system_tray_menu()
{
   hMenu    = CreatePopupMenu();
   hSubMenu = CreatePopupMenu();  
   
   AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "Menu Item");
   			
   AppendMenu(hSubMenu, MF_STRING, ID_SUBMENU_ITEM, "Sub Menu Item");

   AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
   
   AppendMenu(hMenu, MF_STRING, ID_RESTORE, "Restore");   
      			
   AppendMenu(hMenu, MF_STRING, ID_MINIMIZE_TO_TRAY, "Minimize to tray");     
    
   AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
   			
   AppendMenu(hMenu, MF_STRING, ID_MENU_EXIT, "Exit");	

return 0;	
}


long CallbackMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
         case WM_COMMAND:
         {
              switch(wParam)
              {
          	   case ID_RESTORE: 
                   {
            	      minimized_to_tray = false;
            	  
                      ShowWindow(hWnd, SW_RESTORE);
                
                /* show the tray icon.*/
                   
                      Shell_NotifyIcon(NIM_ADD, &_nid); 
                 
                /* disable the "restore" menu item */
         
                      EnableMenuItem(hMenu,ID_RESTORE, MF_GRAYED);   
                      
                /* enable the "minimize to tray" menu item */
                
                      EnableMenuItem(hMenu,ID_MINIMIZE_TO_TRAY, MF_ENABLED);  
                    }
                    break;
 
 
            case ID_MINIMIZE_TO_TRAY: 
            {
                 minimized_to_tray = true;
                 
                 ShowWindow(hWnd, SW_HIDE);
               
                /* show the tray icon.*/
               
                 Shell_NotifyIcon(NIM_ADD, &_nid); 
                     
                /* disable the "minimize to tray" menu item */
         
                 EnableMenuItem(hMenu,ID_MINIMIZE_TO_TRAY, MF_GRAYED);  
               
                /* enable the "restore" menu item */
                
                 EnableMenuItem(hMenu,ID_RESTORE, MF_ENABLED);  
            }
            break; 
            
                       
            case ID_SUBMENU_ITEM: 
            {
                 MessageBox(hWnd, "System Tray Icon Example", "", MB_OK);
            }
            break;
            
            
            case ID_MENU_EXIT: 
            {
               _exit();
            }
            break;
         }
     }
     break;
     
     case WM_SIZE:
     {
        if(wParam == SIZE_MINIMIZED)
        {
           minimized = true; 
        }
     }
     break;

     case WM_CLOSE:
     {
         minimized_to_tray = true;

         ShowWindow(hWnd, SW_HIDE);
         
         /* show the tray icon.*/
         
         Shell_NotifyIcon(NIM_ADD, &_nid); 
         
         /* disable the "minimize to tray" menu item */
         
         EnableMenuItem(hMenu,ID_MINIMIZE_TO_TRAY, MF_GRAYED);   
         
         /* enable the "restore" menu item */
                
         EnableMenuItem(hMenu,ID_RESTORE, MF_ENABLED);          
     }
     break;
 }


 if(message == _nid.uCallbackMessage) 
 {
    switch(lParam)
    {
   	/* (if minimized or minimize to tray) double clicking the icon shows the window */
     	
       case WM_LBUTTONDBLCLK: 
       {
      	  if(minimized_to_tray)
          {
             minimized_to_tray = false;
             
             ShowWindow(hWnd, SW_SHOW);
        
          /* delete the tray icon.*/
      
             Shell_NotifyIcon(NIM_DELETE, &_nid);
      
             EnableMenuItem(hMenu,ID_RESTORE, MF_ENABLED);  
           }
           
           if(minimized)
           { 
             minimized = false;
            
             ShowWindow(hWnd, SW_RESTORE);
           }
       }
    	 break;
    	 
       case WM_RBUTTONUP: 
       {
         POINT cursor_position;
        
         GetCursorPos(&cursor_position);
  
         TrackPopupMenu(hMenu, 0, cursor_position.x, cursor_position.y, 0, hWnd, 0);
       }
       break;
    }
 }
  
return 0;
}




void disable_close()
{
  return;
}


int main()
{

   video_window(nullvector,nullvector,112,"System Tray Icon Example");

   mouse_pointer = 2;

  _nid.cbSize = sizeof(NOTIFYICONDATA); 
  _nid.hWnd = hWnd; 
  _nid.uID = WM_USER + 2;
  _nid.uFlags = NIF_TIP | NIF_ICON | NIF_MESSAGE; 
  _nid.uCallbackMessage = WM_USER + 1; 
  
  _nid.hIcon = (HICON)LoadImage(GetModuleHandle(NULL),"icon.ico", IMAGE_ICON, 16, 16, LR_SHARED | LR_LOADFROMFILE | LR_VGACOLOR); 
 
   strcpy(_nid.szTip, "System Tray Icon Menu"); 


   Shell_NotifyIcon(NIM_ADD, &_nid);
	
	
   system_tray_menu();


  /* disable the "restore" menu item */
  	
	EnableMenuItem(hMenu,ID_RESTORE, MF_GRAYED);  



	on_scanmessage = CallbackMessage;
	
	
	
	while(1)
	{
		/* clicking the close icon will hide the application instead of closing it*/
		
  	   on_close = disable_close; 
			
	   if(key_esc){
          _exit();
	   } 

	wait(1);
	}

return 0;	
}