Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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..
// StealthDlg.cpp : 定义应用程序的入口点。
//
#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);
 	// TODO: 在此放置代码。
	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;
}
//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
/*	INITCOMMONCONTROLSEX initCommCtl;
	initCommCtl.dwSize = sizeof(initCommCtl);
	initCommCtl.dwICC = ICC_STANDARD_CLASSES;
	InitCommonControlsEx( &initCommCtl );*/
    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 /*if ( ullVersion == MAKEDLLVERULL(6,0,0,0) )*/
	{
		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 );
	}
}
Posted

1 solution

Hello, you forgot to set the szTip member of NOTIFYICONDATA struct:

niData.uFlags |= NIF_TIP;
_tcscpy_s(niData.szTip,sizeof(niData.szTip)/sizeof(niData.szTip[0]),__TEXT("Hello world"));

Regards.
 
Share this answer
 
Comments
Leasangle 15-Mar-11 21:26pm    
sh*t.I made a elementary mistake.However,thanks for remind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900