unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ShellAPI, AppEvnts;
const
IDI_TRAYICON = WM_USER + 10;
TRAY_CALLBACK = WM_USER + 240;
type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
procedure ApplicationEvents1Minimize(Sender: TObject);
private
nTrayIcon : tnotifyicondata;
procedure pNotifyIcon(var msg : TMessage); message TRAY_CALLBACK;
procedure ActiveTrayIcon;
procedure ReleaseTrayIcon;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ActiveTrayIcon;
begin
nTrayIcon.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nTrayIcon.uID := uint(IDI_TRAYICON);
nTrayIcon.cbSize := SizeOf(tnotifyicondata);
nTrayIcon.uCallbackMessage := TRAY_CALLBACK;
nTrayIcon.Wnd := Handle;
nTrayIcon.hIcon := Application.Icon.Handle;
StrPCopy(nTrayIcon.sztip, Application.Title + '(单击恢复)');
Shell_NotifyIcon(nim_add, @nTrayIcon);
Visible := false;
Application.Minimize;
end;
procedure TForm1.ReleaseTrayIcon;
begin
nTrayIcon.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
nTrayIcon.uID := uint(IDI_TRAYICON);
nTrayIcon.cbSize := SizeOf(tnotifyicondata);
nTrayIcon.uCallbackMessage := TRAY_CALLBACK;
nTrayIcon.Wnd := Handle;
nTrayIcon.hIcon := Application.Icon.Handle;
StrPCopy(nTrayIcon.sztip, Application.Title + '(单击恢复)');
Shell_NotifyIcon(nim_delete, @nTrayIcon);
Visible := True;
Application.Restore;
Application.BringToFront;
end;
procedure TForm1.pNotifyIcon(var msg : TMessage);
begin
if msg.LParam = WM_LBUTTONUP {WM_LBUTTONDBLCLK} then
ReleaseTrayIcon;
end;
procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
ActiveTrayIcon;
end;
end.