procedure TForm1.WMMyTrayIconCallBack(var Msg: TMessage);
begin
case Msg.LParam of
WM_LBUTTONDBLCLK :
begin
visible := not visible;
Application.ShowMainForm := visible;
SetForegroundWindow(Application.Handle);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
IconIndex := 0;
CurrentIcon := TIcon.Create;
ImageList1.GetIcon(IconIndex,CurrentIcon);
MyTrayIcon.cbSize := Sizeof(TNotifyIconDataA);
MyTrayIcon.Wnd := handle;
MyTrayIcon.uId := 1;
MyTrayIcon.uFlags := NIF_ICON OR NIF_TIP OR NIF_MESSAGE;
//MyTrayIcon.uCallbackMessage := WM_MYTRAYICONCALLBACK;
//MyTrayIcon.hIcon := Application.Icon.Handle;
MyTrayIcon.hIcon := CurrentIcon.Handle;
MyTrayIcon.szTip := '托盘示例程序';
if not Shell_NotifyIcon(NIM_ADD,@MyTrayIcon) then
ShowMessage('创建托盘图标失败!');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,@MyTrayIcon);
CurrentIcon.Free;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
ImageList1.GetIcon(IconIndex,CurrentIcon);
Application.Icon := CurrentIcon;
if IconIndex < 9 then
Inc(IconIndex)
else
IconIndex := 0;
MyTrayIcon.hIcon := CurrentIcon.Handle;
if not Shell_NotifyIcon(NIM_MODIFY,@MyTrayIcon) then
ShowMessage('修改托盘图标失败!');
end;