I am trying to add an icon to notification area.I see the article which URL is
Basic use of Shell_NotifyIcon in Win32[
^] and try to write code according to it.But I find that my code cannot show the tip.Here's my code.I hope someone can help me.
By the way,sorry for my poor English.
Thanks..
#include "stdafx.h"
#include "StealthDlg.h"
#define MAX_LOADSTRING 100
#define TRAY_ICON_ID 1
#define SWM_TRAYMSG WM_APP
#define SWM_SHOW WM_APP + 1
#define SWM_HIDE WM_APP + 2
#define SWM_EXIT WM_APP + 3
HINSTANCE hInst;
NOTIFYICONDATA niData;
BOOL InitInstance(HINSTANCE, int);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
BOOL OnInitDialog( HWND hwnd );
ULONGLONG GetDllVersion( LPCTSTR lpszDllName );
VOID ShowContextMenu( HWND hwnd );
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
HACCEL hAccelTable;
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_STEALTHDLG));
while (GetMessage(&msg, NULL, 0, 0))
{
if ( !TranslateAccelerator(msg.hwnd, hAccelTable, &msg) || !IsDialogMessage(msg.hwnd,&msg) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
HWND hWnd = CreateDialog( hInst,MAKEINTRESOURCE(IDD_STEALTHDLG_DIALOG),NULL,(DLGPROC)DlgProc );
if (!hWnd)
{
return FALSE;
}
ZeroMemory( &niData,sizeof(NOTIFYICONDATA) );
ULONGLONG ullVersion = GetDllVersion(_T("Shell32.dll"));
if ( ullVersion >= MAKEDLLVERULL(6,0,6,0) )
{
niData.cbSize = sizeof(NOTIFYICONDATA);
}
else
{
niData.cbSize = NOTIFYICONDATA_V2_SIZE;
}
niData.hWnd = hWnd;
niData.uID = TRAY_ICON_ID;
niData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
niData.hIcon = (HICON)LoadImage( hInst,MAKEINTRESOURCE(IDI_STEALTHDLG),IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),LR_DEFAULTCOLOR );
niData.uCallbackMessage = SWM_TRAYMSG;
lstrcpyn( niData.szInfo,_T("Hers's the test for Shell_NotifyIcon."),sizeof(niData.szInfo)/sizeof(TCHAR) );
Shell_NotifyIcon( NIM_ADD,&niData );
if ( niData.hIcon && DestroyIcon(niData.hIcon) )
{
niData.hIcon = NULL;
}
ShowWindow( hWnd,SW_SHOW );
return TRUE;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
INT_PTR CALLBACK DlgProc( HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam )
{
switch( uMsg )
{
case SWM_TRAYMSG:
switch( lParam )
{
case WM_LBUTTONDBLCLK:
ShowWindow( hwndDlg,SW_RESTORE );
break;
case WM_RBUTTONDOWN:
case WM_CONTEXTMENU:
ShowContextMenu(hwndDlg);
}
break;
case WM_SYSCOMMAND:
if ( ( wParam & 0xFFF0 ) == SC_MINIMIZE )
{
ShowWindow( hwndDlg,SW_MINIMIZE );
return 1;
}
else if ( wParam == IDM_ABOUT )
DialogBox( hInst,MAKEINTRESOURCE(IDD_ABOUTBOX),hwndDlg,(DLGPROC)About );
break;
case WM_COMMAND:
switch( LOWORD(wParam) )
{
case SWM_SHOW:
ShowWindow( hwndDlg,SW_RESTORE );
break;
case SWM_HIDE:
case IDOK:
ShowWindow( hwndDlg,SW_HIDE );
break;
case SWM_EXIT:
DestroyWindow( hwndDlg );
break;
case IDM_ABOUT:
DialogBox( hInst,MAKEINTRESOURCE(IDD_ABOUTBOX),hwndDlg,(DLGPROC)About );
break;
}
return 1;
case WM_INITDIALOG:
return OnInitDialog( hwndDlg );
case WM_CLOSE:
DestroyWindow( hwndDlg );
break;
case WM_DESTROY:
niData.uFlags = 0;
Shell_NotifyIcon( NIM_DELETE,&niData );
PostQuitMessage( 0 );
break;
}
return 0;
}
BOOL OnInitDialog( HWND hwnd )
{
HMENU hMenu = GetSystemMenu( hwnd,FALSE );
if ( hMenu )
{
AppendMenu( hMenu,MF_SEPARATOR,0,NULL );
AppendMenu( hMenu,MF_STRING,IDM_ABOUT,_T("About") );
}
HICON hBigIcon = (HICON)LoadImage( hInst,MAKEINTRESOURCE(IDI_STEALTHDLG),IMAGE_ICON,0,0,LR_SHARED|LR_DEFAULTSIZE);
HICON hSmallIcon = (HICON)LoadImage( hInst,MAKEINTRESOURCE(IDI_SMALL),IMAGE_ICON,0,0,LR_DEFAULTSIZE );
SendMessage( hwnd,WM_SETICON,ICON_BIG,(LPARAM)hBigIcon );
SendMessage( hwnd,WM_SETICON,ICON_SMALL,(LPARAM)hSmallIcon );
return TRUE;
}
ULONGLONG GetDllVersion( LPCTSTR lpszDllName )
{
ULONGLONG ullVersion;
HINSTANCE hInstDll;
hInstDll = LoadLibrary( lpszDllName );
if ( NULL != hInstDll )
{
DLLGETVERSIONPROC pDllGetVersion;
pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress( hInstDll,"DllGetVersion" );
if ( NULL != pDllGetVersion )
{
DLLVERSIONINFO dvi;
HRESULT hr;
ZeroMemory( &dvi,sizeof(DLLVERSIONINFO) );
dvi.cbSize = sizeof(dvi);
hr = (*pDllGetVersion)(&dvi);
if ( SUCCEEDED(hr) )
{
ullVersion = MAKEDLLVERULL(dvi.dwMajorVersion,dvi.dwMinorVersion,dvi.dwBuildNumber,dvi.dwPlatformID);
}
}
FreeLibrary( hInstDll );
}
else
{
return NULL;
}
return ullVersion;
}
VOID ShowContextMenu( HWND hwnd )
{
HMENU hPopMenu = CreatePopupMenu();
POINT pt;
GetCursorPos( &pt );
if ( hPopMenu != NULL )
{
if ( IsWindowVisible( hwnd ) )
{
InsertMenu( hPopMenu,-1,MF_BYPOSITION,SWM_HIDE,_T("Hide") );
}
else
{
InsertMenu( hPopMenu,-1,MF_BYPOSITION,SWM_SHOW,_T("Show") );
}
InsertMenu( hPopMenu,-1,MF_BYPOSITION,SWM_EXIT,_T("Exit") );
SetForegroundWindow( hwnd );
TrackPopupMenu( hPopMenu,TPM_BOTTOMALIGN,pt.x,pt.y,0,hwnd,NULL );
DestroyMenu( hPopMenu );
}
}