procedure TForm1.FormCreate(Sender: TObject);
begin
//将trayicon调用到程序中,如果失败终止程序
appicon:=Ticon.Create;
appicon.Handle:=loadIcon(hinstance,'trayicon');
if appicon.Handle=0 then
begin
messagebox(handle,'不能打开图标文件!','提示',mb_ok);
application.Terminate;
exit;
end;
//将nid数据结构初始化
with nid do
begin
cbsize:=sizeof(tnotifyicondata);
wnd:=handle;
uId:=trayiconid;
uflags:=NIF_MESSAGE OR NIF_ICON OR NIF_TIP;
UCALLBACKMESSAGE:=0;
hicon:=appicon.Handle;
strpcopy(sztip,'the test app');
end;
//加载图标
shell_notifyicon(NIM_ADD,@nid);
end;
procedure tform1.MessageHandler(var msg:tmessage);
var
point:TPoint;
begin
//判断收到消息是不是由trayicon发出如果是
if msg.WParam=trayiconid then
//判断消息类型 做出相应处理
case msg.LParam of
//鼠标双击
WM_LBUTTONDBLCLK:
begin
//如果显示则隐藏,如果隐藏则显示
if visible then
hide
else
show;
end;
//右键单击
WM_RBUTTONUP :
begin
//用API函数getcursorpos将鼠标当前坐标写到定义的point结构中
GetCursorPos(point);
//弹出菜单
popupmenu1.Popup(point.X,point.y);
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//推出程序时候删除任务栏的图标
shell_notifyicon(NIm_delete,@nid);
appicon.Free;
end;