procedure TForm1.WMTrayMessage(var msg: TMessage);
var
p:TPoint;
begin
if (Msg.LParam=WM_LBUTTONDBLCLK) then
begin
MessageDlg('您在任务栏图标上按双击左键', mtInformation, [mbOK], 0);
end;
if msg.LParam=WM_RButtonDown then
begin
GetCursorPos(p); //取光标位置
PopupMenu1.Popup(p.x,p.y); //弹出菜单
end;
if (msg.LParam=WM_LButtonDown) and Form1.Enabled then
begin
Form1.Visible:=not Form1.Visible;
// Shell_NotifyIcon(NIM_DELETE,@NID);
end;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then Shell_NotifyIcon(NIM_ADD,@NID) else Shell_NotifyIcon(NIM_DELETE,@NID);
{Shell_NotifyIcon是tray区编程函数,它只有两个参数
第一个参数可为:nim_add :向tray区增加一个图标
nim_delete:删除tray区一个图标
nim_modify:修改tray区一个图标
第二个参数包含的信息可以用于系统对任务tray区的处理
}
end;
procedure TForm1.Hello1Click(Sender: TObject);
begin
MessageDlg('Hello!', mtInformation, [mbOK], 0);
end;
procedure TForm1.World1Click(Sender: TObject);
begin
MessageDlg('World!', mtInformation, [mbOK], 0);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CheckBox1.Checked := False;
end;
procedure TForm1.wmsyscommand(var Msg: TWMSysCommand);
begin
if (Msg.CmdType=SC_MINIMIZE) or (Form1.Enabled=false) THEN
begin
Shell_NotifyIcon(NIM_ADD,@NID);
Form1.Hide;
end
else
INHERITED;
//如果消息是mimimize,就处理消息,否则,让它继续传下去。
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
NID.cbSize:=SizeOf(TNotifyIconData);
NID.hIcon:=Application.Icon.Handle;
NID.szTip:='欢迎使用任务栏图标';
NID.uCallbackMessage:=WM_TrayMessage;
NID.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
{uflags描述那些项有效
nif_icon:hicon有效
nif_message:ucallbackmessage有效
nif_tip:tip有效}
NID.uID:=0;
//uID域是程序员定义的一个唯一标识符。如果一个应用程序有多个托盘图标,则每个图标需要有唯一的I D
NID.Wnd:=Handle;{tray区返回消息的对象,这些消息将用户对tray区图标的操作通知给程序}
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE,@NID);
end;