{
Use the AddSysTrayIcon procedure to add icon to notification area
(in FormCreate, for example), and use the ShowBalloonTips procedure
when ever you want(of cause must after you called AddSysTrayIcon procedure),
and finally call DeleteSysTrayIcon procedure to remove icon from
notification area after your using.
Add propriety code to the callback message handler.
procedure TForm1.SysTrayIconMsgHandler(var Msg: TMessage); begin
case Msg.lParam of WM_MOUSEMOVE:;
WM_LBUTTONDOWN:;
WM_LBUTTONUP:;
WM_LBUTTONDBLCLK:;
WM_RBUTTONDOWN:;
WM_RBUTTONUP:;
WM_RBUTTONDBLCLK:; //followed by the new messages NIN_BALLOONSHOW: {Sent when the balloon is shown} ShowMessage('NIN_BALLOONSHOW');
NIN_BALLOONHIDE: {Sent when the balloon disappears?Rwhen the icon is deleted,
for example. This message is not sent if the balloon is dismissed because of
a timeout or mouse click by the user. } ShowMessage('NIN_BALLOONHIDE');
NIN_BALLOONTIMEOUT: {Sent when the balloon is dismissed because of a timeout.} ShowMessage('NIN_BALLOONTIMEOUT');
NIN_BALLOONUSERCLICK: {Sent when the balloon is dismissed because the user clicked the mouse.
Note: in XP there's Close button on he balloon tips, when click the button,
send NIN_BALLOONTIMEOUT message actually.} ShowMessage('NIN_BALLOONUSERCLICK'); end; end;
{AddSysTrayIcon procedure add an icon to notification area} procedure TForm1.AddSysTrayIcon; begin IconData.cbSize := SizeOf(IconData);
IconData.Wnd := AllocateHWnd(SysTrayIconMsgHandler); {SysTrayIconMsgHandler is then callback message' handler} IconData.uID := 0;
IconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
IconData.uCallbackMessage := TRAY_CALLBACK; //user defined callback message IconData.hIcon := Application.Icon.Handle; //an Icon's Handle IconData.szTip := 'Please send me email.'; if not Shell_NotifyIcon(NIM_ADD, @IconData) then ShowMessage('add fail'); end;
{ShowBalloonTips procedure carry out the new feature: Balloon Tips} procedure TForm1.ShowBalloonTips; var TipInfo, TipTitle: string; begin IconData.cbSize := SizeOf(IconData);
IconData.uFlags := NIF_INFO;
TipInfo := 'Please send me email.';
strPLCopy(IconData.szInfo, TipInfo, SizeOf(IconData.szInfo) - 1);
IconData.DUMMYUNIONNAME.uTimeout := 3000;
TipTitle := 'Happyjoe@21cn.com';
strPLCopy(IconData.szInfoTitle, TipTitle, SizeOf(IconData.szInfoTitle) - 1);
IconData.dwInfoFlags := NIIF_INFO; //NIIF_ERROR; //NIIF_WARNING; Shell_NotifyIcon(NIM_MODIFY, @IconData); {in my testing, the following code has no use} IconData.DUMMYUNIONNAME.uVersion := NOTIFYICON_VERSION; if not Shell_NotifyIcon(NIM_SETVERSION, @IconData) then ShowMessage('setversion fail'); end;
{here's the deletion procedure} procedure TForm1.DeleteSysTrayIcon; begin DeallocateHWnd(IconData.Wnd); if not Shell_NotifyIcon(NIM_DELETE, @IconData) then ShowMessage('delete fail'); end;
procedure TForm1.FormCreate(Sender: TObject); begin AddSysTrayIcon;
ShowBalloonTips; end;
procedure TForm1.FormDestroy(Sender: TObject); begin DeleteSysTrayIcon; end;