下面是我的主代码,资源文件和头文件就不贴了。我用纯Win32API的,MFC的不要说。我用的VS2008写的。
程序运行没错误,可是我想在托盘图标上点左键的时候它出个气泡提示。但是出不来。。应该怎么改?谢谢啦。
//#define _WIN32_IE 0x0500
#include <windows.h>
#include <shlwapi.h>
#include "resource.h"
//宏定义
#define TRAY_MSG (WM_USER+1)
#define TRAY_BALLOON  (WM_USER+2)
//函数声明
INT_PTR CALLBACK DialogProc( HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);//全局变量
NOTIFYICONDATA NotifyData; 
HINSTANCE hWinMain;
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
int nCmdShow)
{
hWinMain=hInstance;
return DialogBoxParam( hInstance,
MAKEINTRESOURCE(IDD_DIALOG1),
NULL,
DialogProc,
0);
}INT_PTR CALLBACK DialogProc( HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
static int counter=0;
DWORD error;
WCHAR temp[50]; 
HMODULE hModule;
DLLGETVERSIONPROC  GetDllVersion;
DLLVERSIONINFO DllInfo;
HMENU hRmenu;
POINT curPoint;
WORD exitWord;
switch(uMsg)
{
case WM_INITDIALOG:
MoveWindow(hwndDlg,300,200,600,300,FALSE);
// ZeroMemory(&NotifyData,sizeof(NotifyData));
NotifyData.cbSize=sizeof(NotifyData);
//NotifyData.uVersion=NOTIFYICON_VERSION;
NotifyData.hWnd=hwndDlg;
NotifyData.uID=IDI_ICON1;
NotifyData.uFlags =NIF_ICON|NIF_MESSAGE|NIF_TIP;
NotifyData.uCallbackMessage =TRAY_MSG;
//NotifyData.hIcon =LoadIcon(hWinMain,MAKEINTRESOURCE(IDI_ICON1));
NotifyData.hIcon =LoadIcon(NULL,IDI_APPLICATION);
lstrcpy(NotifyData.szTip,L"hello");
//wsprintf(temp,L"%d,%d,%d",_WIN32_IE,_WIN32_WINNT,NTDDI_VERSION);
//默认指定的版本相当高
//wsprintf(temp,L"%ld,%ld",0x700,0x200);
//MessageBox(hwndDlg,temp,L"title",0);
/*
if(Shell_NotifyIcon(NIM_SETVERSION,&NotifyData)==FALSE)
{//这个语句是会执行的...
error=GetLastError();
wsprintf(temp,L"error=%d",error);
MessageBox(hwndDlg,temp,L"title",0); };
*/
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
Shell_NotifyIcon(NIM_DELETE,&NotifyData);
EndDialog(hwndDlg,TRUE);
return TRUE;
case IDCANCEL://点关闭按钮相当于IDCANCEL
if(counter<1)
{
counter++;
return TRUE;
}
EndDialog(hwndDlg,FALSE);
return TRUE;
case IDC_BUTTON1:
hModule=LoadLibrary(L"shell32.dll");
if(!hModule)
{
MessageBox(hwndDlg,L"shell32.dll load failed.",L"title",0);
return TRUE;
}
GetDllVersion=(DLLGETVERSIONPROC)GetProcAddress(hModule,"DllGetVersion");
if(!GetDllVersion)
{
MessageBox(hwndDlg,L"DllGetVersion no exist.",L"title",0);
return TRUE;
}
DllInfo.cbSize =sizeof(DllInfo);
GetDllVersion(&DllInfo);
wsprintf(temp,L"shell32 major version:%d,minor version:%d",DllInfo.dwMajorVersion ,DllInfo.dwMinorVersion);
MessageBox(hwndDlg,temp,L"title",0);
return TRUE;
case IDC_BUTTON2://显示托盘图标
Shell_NotifyIcon(NIM_ADD,&NotifyData);
return TRUE;
case IDC_BUTTON3://hidden tray icon.
Shell_NotifyIcon(NIM_DELETE,&NotifyData);
return TRUE;
}//WM_COMMAND
case TRAY_MSG:
switch(lParam)
{
case WM_LBUTTONDOWN:
//NotifyData.cbSize = sizeof(NOTIFYICONDATA);
//NotifyData.hWnd = this->Handle;
//NotifyData.uID=0;
NotifyData.uFlags = NIF_INFO;
//NotifyData.hIcon=Application->Icon->Handle;
//lstrcpy(NotifyData.szTip, TEXT("气球提示"));
lstrcpy(NotifyData.szInfo, TEXT("Your message text goes here."));
lstrcpy(NotifyData.szInfoTitle, TEXT("标题"));
//NotifyData.uTimeout = 15000; // in milliseconds
NotifyData.uVersion=NOTIFYICON_VERSION;
//NotifyData.dwState=NIS_SHAREDICON;
//NotifyData.dwStateMask=0;
NotifyData.dwInfoFlags=NIIF_INFO;
NotifyData.uCallbackMessage= TRAY_MSG;
//MessageBox(hwndDlg,L"here comes.",L"title",0);
//这是一个自定义消息,当你在提示框上按下鼠标后,
//会给IconData.hWnd发这个自定义消息,其lParam是NIN_BALLOONUSERCLICK,
//如此你可以响应这个消息。
if(Shell_NotifyIcon(NIM_SETVERSION, &NotifyData)==FALSE)
{
MessageBox(hwndDlg,L"set version failed.",L"title",0);
}
NotifyData.uTimeout =15000;
Shell_NotifyIcon(NIM_MODIFY,&NotifyData);
//Shell_NotifyIcon(NIM_DELETE, &IconData);删除提示框
//MessageBox(hwndDlg,L"haha,you press left button.",L"title",0);
break;
case WM_RBUTTONDOWN:
GetCursorPos(&curPoint);
hRmenu=GetSubMenu(LoadMenu(hWinMain,MAKEINTRESOURCE(IDR_MENU1)),0);
SetForegroundWindow(hwndDlg);
exitWord=TrackPopupMenu( hRmenu,
TPM_RIGHTALIGN|TPM_BOTTOMALIGN|TPM_RETURNCMD|TPM_LEFTBUTTON,
curPoint.x,
curPoint.y,
0,
hwndDlg,
0); PostMessage(hwndDlg,WM_COMMAND,exitWord,0);
//MessageBox(hwndDlg,L"你在托盘图标上按下了鼠标右键",L"title",0);
break;
case NIN_BALLOONUSERCLICK:
MessageBox(hwndDlg,L"you press on balloon.",L"titile",0);
break;
}
return TRUE;
default:
return FALSE;
}
//return FALSE;
}