通常在项目中我们需要在托盘图标中有气泡显示,像下面这样:
关于第一个图片的显示: 我们 需要使用 工具提示控件,先进行InitCommonControlsEx,然后CreateWindowEx,然后SendMessage激活该控件。
关于第二个图片的显示:我们需要NOTIFYICONDATA结构体,然后设置dwInfoFlags标志为NIIF_INFO,就可以有消息提示。
#define _WIN32_IE 0x0500
#include <windows.h>
#include "commctrl.h"
#include "TCHAR.h"
class gToolTip
{
public:
static BOOL AddTip( HWND,HINSTANCE,TCHAR*,UINT,BOOL = FALSE);
/*static*/ void PutInTaskBar(HWND,HINSTANCE,HICON,UINT=INFINITE);
NOTIFYICONDATA nfd;
void DeleteTaskBarIcon();
};
#include "gToolTip.h"
#include "resource.h"
#pragma comment(lib,"comctl32.lib")
BOOL gToolTip::AddTip(HWND hWnd,HINSTANCE hInst,TCHAR *Tip,UINT id , BOOL Balloon)
{
INITCOMMONCONTROLSEX icc;
HWND hwndTip;
TOOLINFO ti;
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC =ICC_BAR_CLASSES | ICC_TAB_CLASSES | ICC_WIN95_CLASSES ;
InitCommonControlsEx(&icc);
if(Balloon)//If you have choosen the Boolen Toop Tip will set the Windows style according to that
{
hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP |TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hWnd, NULL, hInst,
NULL);
}
else
{
hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX |TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hWnd, NULL, hInst,
NULL);
}
SendMessage(hwndTip,TTM_ACTIVATE,TRUE,0); //Will Active the Tool Tip Control
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
ti.hwnd = hWnd; //Handle of the window in which the Contol resides
ti.uId =(UINT)GetDlgItem(hWnd,id); //ID of the Cotrol for which Tool Tip will be Displyed
ti.hinst = hInst;
ti.lpszText = Tip; //Tip you want to Display;
ti.rect.left = ti.rect.top = ti.rect.bottom = ti.rect.right = 0;
if(!SendMessage(hwndTip,TTM_ADDTOOL,0,(LPARAM)&ti)){ //Will add the Tool Tip on Control
MessageBox(NULL,L"Couldn't create the ToolTip control.",L"Error",MB_OK);
}
return TRUE;
}
void gToolTip::PutInTaskBar(HWND hWndDlg,HINSTANCE hInst,HICON hIcon,UINT TimeOut)
{
nfd.cbSize = sizeof(NOTIFYICONDATA);
nfd.hWnd = hWndDlg;
wcscpy(nfd.szTip,L"Tip.. " );
wcscpy(nfd.szInfo, _T("szInfo"));
wcscpy(nfd. szInfoTitle, _T(" szInfoTitle."));
nfd.uTimeout = TimeOut*1000;
// nfd.uCallbackMessage
nfd.uFlags = NIF_ICON | NIF_TIP | NIF_INFO;
nfd.dwInfoFlags =NIIF_INFO;
nfd.hIcon = hIcon;
if( !Shell_NotifyIcon(NIM_ADD,&nfd))
MessageBox(NULL,L"Error ",L"" ,0);
}
void gToolTip::DeleteTaskBarIcon()
{
Shell_NotifyIcon(NIM_DELETE,&nfd);
}
调用的函数:
#include "ToolTipDemo.h"
void ShutDown(void);
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),hWnd, reinterpret_cast<DLGPROC>(MyDialogProc));
return (int) 0;
}
LRESULT CALLBACK MyDialogProc(HWND hWndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_INITDIALOG:
{
/*TCHAR tip[100]= _T("Enter Your User Name Here.User Name is your Email ID");
gToolTip::AddTip(hWndDlg,hInst,tip,IDC_EDIT1,TRUE);
wcscpy(tip, _T("Enter Your Password"));
gToolTip::AddTip(hWndDlg,hInst,tip,IDC_EDIT2);
gToolTip::AddTip(hWndDlg,hInst,_T("Press Enter to Start") ,IDOK);
gToolTip::AddTip(hWndDlg,hInst,_T("Press Cancel To Exit") ,IDCANCEL,TRUE);*/
}
case WM_COMMAND:
if(LOWORD(wParam == IDOK))
{
HICON hIcon = LoadIcon(hInst,(LPCWSTR)IDI_ICON);
gToolTip tooltip;
tooltip.PutInTaskBar(hWndDlg,hInst,hIcon,1);
/* Sleep(3000);
tooltip.DeleteTaskBarIcon();*/
}
if(LOWORD(wParam == IDCANCEL))
{
EndDialog(hWndDlg,0);
}
}
return FALSE;
}