unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, shellapi, //ADD shellapi
AppEvnts, ImgList, Menus;
const
ghy_tray=wm_user+2;
type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
ImageList1: TImageList;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
procedure ApplicationEvents1Minimize(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure WMSysCommand(var msg: TMessage);message wm_syscommand;
public
{ Public declarations }
procedure mytray(var Msg: TMessage); message ghy_tray;
procedure BeforeRestore;
end;
var
Form1: TForm1;
tray1,tray2:TNotifyIconData;
ico1,ico2:ticon;
tags:boolean;
implementation
{$R *.DFM}
procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
tags:=false;
ShowWindow(Application.Handle,SW_HIDE);
tray2.cbSize:=sizeof(tray2);
tray2.Wnd:=form1.Handle;
tray2.uID:=0;
tray2.uFlags:=NIF_ICON or NIF_TIP or NIF_MESSAGE;
tray2.uCallbackMessage:=ghy_tray;
tray2.hIcon:=ico2.Handle;
tray2.szTip:='我的托盘程序';
if not Shell_NotifyIcon(NIM_modify,@tray2) then
begin
ShowMessage('no');
end;
end;
procedure TForm1.BeforeRestore;
begin
tags:=true;
ShowWindow(Application.Handle,SW_SHOW);
ico1:=ticon.Create;
ico2:=ticon.create;
imagelist1.GetIcon(0,ico1);
imagelist1.geticon(1,ico2);
tray1.cbSize:=sizeof(tray1);
tray1.Wnd:=form1.Handle;
tray1.uID:=0;
tray1.uFlags:=NIF_ICON or NIF_TIP or NIF_MESSAGE;
tray1.uCallbackMessage:=ghy_tray;
tray1.hIcon:=ico1.Handle;
tray1.szTip:='我的托盘程序';
if not Shell_NotifyIcon(NIM_modify,@tray1) then
begin
ShowMessage('no');
end;
end;
procedure TForm1.mytray(var Msg: TMessage);
var
pt:tpoint;
begin
GetCursorPos(pt);
case msg.lParam of
WM_LBUTTONDOWN:
begin
//鼠标左键被按下
if tags=true then
begin
application.Minimize;
form1.Hide;
end
else
begin
BeforeRestore;
application.Restore;
end;
end;
WM_LBUTTONUP:
begin
//释放鼠标左键
end;
wm_rbuttondown:
begin
//鼠标右键被按下
SetForegroundWindow(Form1.Handle);
form1.PopupMenu1.Popup(pt.x,pt.y);
end
else//调用父类的WndProc方法处理其它消息
inherited;
end;
end;
procedure TForm1.WMSysCommand(var msg: TMessage);
begin
if msg.WParam = SC_MINIMIZE then
begin
showwindow(application.handle,sw_hide);
inherited;
end
else
inherited;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
tags:=true;
ShowWindow(Application.Handle,SW_HIDE);
ico1:=ticon.Create;
ico2:=ticon.create;
imagelist1.GetIcon(0,ico1);
imagelist1.geticon(1,ico2);
tray1.cbSize:=sizeof(tray1);
tray1.Wnd:=form1.Handle;
tray1.uID:=0;
tray1.uFlags:=NIF_ICON or NIF_TIP or NIF_MESSAGE;
tray1.uCallbackMessage:=ghy_tray;
tray1.hIcon:=ico1.Handle;
tray1.szTip:='我的托盘程序';
if not Shell_NotifyIcon(NIM_ADD,@tray1) then
begin
ShowMessage('no');
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ico1:=ticon.Create;
ico2:=ticon.create;
imagelist1.GetIcon(0,ico1);
imagelist1.geticon(1,ico2);
tray1.cbSize:=sizeof(tray1);
tray1.Wnd:=form1.Handle;
tray1.uID:=0;
tray1.uFlags:=NIF_ICON or NIF_TIP or NIF_MESSAGE;
tray1.uCallbackMessage:=ghy_tray;
tray1.hIcon:=ico1.Handle;
tray1.szTip:='我的托盘程序';
if not Shell_NotifyIcon(NIM_delete,@tray1) then
begin
ShowMessage('no');
end;
end;
procedure TForm1.N1Click(Sender: TObject);
begin
showmessage('you click open');
end;
procedure TForm1.N2Click(Sender: TObject);
begin
showmessage('you click save');
end;
procedure TForm1.N3Click(Sender: TObject);
begin
showmessage('you click saveas');
end;
end.
http://www.cnblogs.com/safezone/articles/1260888.html