Archivo main.cpp
Código
#define _WIN32_WINNT 0x0500 //INPUT #define _WIN32_IE 0x0600 #define ICON_ID 100 #define SYSTRAY_MSG 101 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <iostream> #include "SysTray.h" #include "SysTray_cpp.h" #include "Definiciones.h" using namespace std; int main() { HWND hWnd; SysTray SysT(ICON_ID, hWnd, SYSTRAY_MSG); SysT.SetInSysTray ( "I'm in the SysTray!", LoadIcon ( GetModuleHandle ( NULL ), MAKEINTRESOURCE ( ID_ICON ) ) ); SysT.ShowBalloon("Hola","Pruebaaaa", 3); Activar_Icono(0,0,0,0); /** 0 = Sin Icono 1 = Informacion 2 = Alerta 3 = Error **/ return 0; }
Archivo SysTray.cpp
Código
#define _WIN32_IE 0x0500 #include <windows.h> #include "SysTray.h" // Constructer, sets values passed by the parameters SysTray::SysTray ( int ID, HWND hWnd, int Message ) { _id = ID; // Set the id for the icon _hWnd = hWnd; // The handle to the window _message = Message; // The message sent to the window procedure when something happends } // Function to set the application in the SysTray BOOL SysTray::SetInSysTray ( char* ToolTip, HICON hIcon ) { NOTIFYICONDATA nid; nid.uID = _id; // Id for the icon nid.cbSize = sizeof ( NOTIFYICONDATA ); // Size of structure nid.hWnd = _hWnd; // Handle to the window nid.uFlags = NIF_MESSAGE|NIF_TIP|NIF_ICON; // Options nid.hIcon = hIcon; // The icon nid.uCallbackMessage = _message; // The message lstrcpyn ( nid.szTip, ToolTip, sizeof ( nid.szTip ) ); // The tooltip return Shell_NotifyIcon ( NIM_ADD, &nid ); // Add in the SysTray } // Function to remove the application in the SysTray BOOL SysTray::RemoveFromSysTray () { NOTIFYICONDATA nid; nid.uID = _id; // Id for the icon nid.cbSize = sizeof ( NOTIFYICONDATA ); // Size of structure nid.hWnd = _hWnd; // Handle to the window return Shell_NotifyIcon ( NIM_DELETE, &nid ); // Delete the icon } // Show a tooltip balloon BOOL SysTray::ShowBalloon ( char* Title, char* Message, int Icon ) { NOTIFYICONDATA nid; nid.cbSize = sizeof ( NOTIFYICONDATA ); // Size of structure nid.hWnd = _hWnd; // Handle to parent window nid.uID = _id; // Id of the icon nid.uFlags = NIF_INFO; // Options: show balloon nid.dwInfoFlags = Icon; // Icon for the balloon nid.uTimeout = 1000; // Timeout when the balloon dissapear lstrcpyn ( nid.szInfo, Message, sizeof ( nid.szInfo ) ); // Message lstrcpyn ( nid.szInfoTitle, Title, sizeof ( nid.szInfoTitle ) ); // Title return Shell_NotifyIcon ( NIM_MODIFY, &nid ); // Show balloon }
Archivo SysTray.h
Código
#ifndef _SYSTRAY_H_ #define _SYSTRAY_H_ #include <windows.h> // Extra messages for the balloon #ifndef NIN_BALLOONSHOW // Send when the balloon is shown #define NIN_BALLOONSHOW WM_USER + 2 #endif #ifndef NIN_BALLOONHIDE // When the balloon is hide ( when you press the X button ) #define NIN_BALLOONHIDE WM_USER + 3 #endif #ifndef NIN_BALLOONTIMEOUT // Whe the balloon dissapear #define NIN_BALLOONTIMEOUT WM_USER + 4 #endif #ifndef NIN_BALLOONUSERCLICK // When the user clicks on the balloon #define NIN_BALLOONUSERCLICK WM_USER + 5 #endif class SysTray { public: SysTray ( int ID, HWND hWnd, int Message ); BOOL SetInSysTray ( char* ToolTip, HICON hIcon ); BOOL RemoveFromSysTray (); BOOL ShowBalloon ( char* Title, char* Message, int Icon ); private: int _message; int _id; HWND _hWnd; }; #endif
Archivo SysTray_cpp.h
Código
// SysTrayDemo.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "Definiciones.h" #define MAX_LOADSTRING 100 #define WM_USER_SHELLICON WM_USER + 1 // Global Variables: HINSTANCE hInst; // current instance NOTIFYICONDATA nidApp; HMENU hPopMenu; TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name TCHAR szApplicationToolTip[MAX_LOADSTRING]; // the main window class name BOOL bDisable = FALSE; // keep application state // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int APIENTRY Activar_Icono(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings //LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_SYSTRAYDEMO, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SYSTRAYDEMO)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage are only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SYSTRAYDEMO)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SYSTRAYDEMO); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; HICON hMainIcon; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } hMainIcon = LoadIcon(hInstance,(LPCTSTR)MAKEINTRESOURCE(IDI_SYSTRAYDEMO)); nidApp.cbSize = sizeof(NOTIFYICONDATA); // sizeof the struct in bytes nidApp.hWnd = (HWND) hWnd; //handle of the window which will process this app. messages nidApp.uID = IDI_SYSTRAYDEMO; //ID of the icon that willl appear in the system tray nidApp.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; //ORing of all the flags nidApp.hIcon = hMainIcon; // handle of the Icon to be displayed, obtained from LoadIcon nidApp.uCallbackMessage = WM_USER_SHELLICON; LoadString(hInstance, IDS_APPTOOLTIP,nidApp.szTip,MAX_LOADSTRING); Shell_NotifyIcon(NIM_ADD, &nidApp); return TRUE; } void Init() { // user defined message that will be sent as the notification message to the Window Procedure } // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; POINT lpClickPoint; switch (message) { case WM_USER_SHELLICON: // systray msg callback switch(LOWORD(lParam)) { case WM_RBUTTONDOWN: UINT uFlag = MF_BYPOSITION|MF_STRING; GetCursorPos(&lpClickPoint); hPopMenu = CreatePopupMenu(); InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_ABOUT,_T("About")); if ( bDisable == TRUE ) { uFlag |= MF_GRAYED; } InsertMenu(hPopMenu,0xFFFFFFFF,uFlag,IDM_TEST2,_T("Test 2")); // Test 2 InsertMenu(hPopMenu,0xFFFFFFFF,uFlag,IDM_TEST1,_T("Test 1")); // Test 1 InsertMenu(hPopMenu,0xFFFFFFFF,MF_SEPARATOR,IDM_SEP,_T("SEP")); if ( bDisable == TRUE ) { InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_ENABLE,_T("Enable")); } else { InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_DISABLE,_T("Disable")); } InsertMenu(hPopMenu,0xFFFFFFFF,MF_SEPARATOR,IDM_SEP,_T("SEP")); InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_EXIT,_T("Exit")); SetForegroundWindow(hWnd); TrackPopupMenu(hPopMenu,TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_BOTTOMALIGN,lpClickPoint.x, lpClickPoint.y,0,hWnd,NULL); return TRUE; } break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: //DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_TEST1: // MessageBox(NULL,_T("This is a test for menu Test 1"),_T("Test 1"),MB_OK); break; case IDM_TEST2: // MessageBox(NULL,_T("This is a test for menu Test 2"),_T("Test 2"),MB_OK); break; case IDM_DISABLE: bDisable = TRUE; break; case IDM_ENABLE: bDisable = FALSE; break; case IDM_EXIT: Shell_NotifyIcon(NIM_DELETE,&nidApp); DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; /* case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break;*/ case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
Archivo Definiciones.h
Código
/** Esta cabecera es para definiciones **/ #define ID_MENU 3001 #define ID_SYSTRAY_INFO 3002 #define ID_SYSTRAY_DELETE 3003 #define ID_SYSTRAY_EXIT 3004 #define ID_ICON 4001 #define ID_BMP_CLOSE 5001 #define ID_BMP_ABOUT 5002 /** ---------------------------- **/ #define IDC_MYICON 2 #define IDD_SYSTRAYDEMO_DIALOG 102 #define IDS_APP_TITLE 103 #define IDM_ABOUT 104 #define IDS_APPTOOLTIP 104 #define IDM_EXIT 105 #define IDI_SYSTRAYDEMO 107 #define IDI_SMALL 108 #define IDC_SYSTRAYDEMO 109 #define IDR_MAINFRAME 128 #define ID_S_ 32771 #define ID_S_EXIT 32772 #define ID_SYSTRAYMENU_ 32773 #define ID_SYSTRAYMENU_ENABLE 32774 #define ID_SYSTRAYMENU_DISABLE 32775 #define ID_SYSTRAYMENU_TEST1 32776 #define ID_SYSTRAYMENU_TEST2 32777 #define IDM_DISABLE 32778 #define IDM_ENABLE 32779 #define IDM_TEST2 32780 #define IDM_TEST1 32781 #define ID_SYSTRAYMENU_SEP 32782 #define IDM_SEP 32783 #define IDC_STATIC -1 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NO_MFC 1 #define _APS_NEXT_RESOURCE_VALUE 129 #define _APS_NEXT_COMMAND_VALUE 32784 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 110 #endif #endif
Archivo stdafx.h
Código
// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #pragma once // Modify the following defines if you have to target a platform prior to the ones specified below. // Refer to MSDN for the latest info on corresponding values for different platforms. #ifndef WINVER // Allow use of features specific to Windows XP or later. #define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows. #endif #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. #endif #ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later. #define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later. #endif #ifndef _WIN32_IE // Allow use of features specific to IE 6.0 or later. #define _WIN32_IE 0x0600 // Change this to the appropriate value to target other versions of IE. #endif #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files: #include <windows.h> #include <shellapi.h> // C RunTime Header Files #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <tchar.h> // TODO: reference additional headers your program requires here
Archivo Resource.rc
Código
#include <windows.h> #include "Definiciones.h" ID_MENU MENU BEGIN POPUP "&SysTray" BEGIN MENUITEM "&About", ID_SYSTRAY_INFO MENUITEM "&Delete icon", ID_SYSTRAY_DELETE MENUITEM SEPARATOR MENUITEM "&Exit", ID_SYSTRAY_EXIT END END ID_ICON ICON "MouseMove.ico" ID_BMP_CLOSE BITMAP DISCARDABLE "close.bmp" ID_BMP_ABOUT BITMAP DISCARDABLE "about.bmp" // Creates an Windows XP Style: 1 24 MOVEABLE PURE "Data1.bin" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 2 resource. // #define APSTUDIO_HIDDEN_SYMBOLS #include "windows.h" #undef APSTUDIO_HIDDEN_SYMBOLS ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // Hebrew resources #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_HEB) #ifdef _WIN32 LANGUAGE LANG_HEBREW, SUBLANG_DEFAULT #pragma code_page(1255) #endif //_WIN32 ///////////////////////////////////////////////////////////////////////////// // // Icon // // Icon with lowest ID value placed first to ensure application icon // remains consistent on all systems. IDI_SYSTRAYDEMO ICON "MouseMove.ico" IDI_SMALL ICON "MouseMove.ico" ///////////////////////////////////////////////////////////////////////////// // // Menu // IDC_SYSTRAYDEMO MENU BEGIN POPUP "SysTrayMenu" BEGIN MENUITEM "&About ...", IDM_ABOUT MENUITEM SEPARATOR MENUITEM "Test 1", IDM_TEST1 MENUITEM "Test 2", IDM_TEST2 MENUITEM SEPARATOR MENUITEM "Enable", IDM_ENABLE MENUITEM "Disable", IDM_DISABLE MENUITEM SEPARATOR MENUITEM "E&xit", IDM_EXIT MENUITEM "SEP", IDM_SEP END END ///////////////////////////////////////////////////////////////////////////// // // Accelerator // IDC_SYSTRAYDEMO ACCELERATORS BEGIN "?", IDM_ABOUT, ASCII, ALT "/", IDM_ABOUT, ASCII, ALT END ///////////////////////////////////////////////////////////////////////////// // // Dialog // #ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // TEXTINCLUDE // 1 TEXTINCLUDE BEGIN "Definiciones.h\0" END 2 TEXTINCLUDE BEGIN "#define APSTUDIO_HIDDEN_SYMBOLS\r\n" "#include ""windows.h""\r\n" "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n" "\0" END 3 TEXTINCLUDE BEGIN "\r\n" "\0" END #endif // APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // String Table // STRINGTABLE BEGIN IDS_APP_TITLE "SysTrayDemo" IDS_APPTOOLTIP "SysTray Demo" IDC_SYSTRAYDEMO "SYSTRAYDEMO" END #endif // Hebrew resources ///////////////////////////////////////////////////////////////////////////// #ifndef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 3 resource. // ///////////////////////////////////////////////////////////////////////////// #endif // not APSTUDIO_INVOKED
Ahora os explico, lo único que quiero es que al iniciar el programa (GUI Application), se cree un icono en el System Tray, y se quede ahí mientras el programa va corriendo, y que puedas usar las funciones del Menu al hacer click derecho en el Icono, todo esto sin que el programa tenga interfaz gráfica, es decir, que el programa solo exista en el Tray, y que si quieres usar alguna función, la llames desde el menú del icono... Pero la liada de código es tan grande que no se por donde meter mano... a ver si alguien puede arreglar el código, comentar lo que hace cada cosa y quitar archivos y todos los procedimientos que no sean necesarios, ya que hay funciones que hacen lo mismo, se crean dos iconos en el Tray, etc... así el código lo pueden usar ustedes también si quieren...
Gracias y Saludos