procedure TForm1.Timer1Timer(Sender: TObject);
var
st:SystemTime;
begin
//每秒钟更新一次图标:图标0显示秒数的十位,
图标1显示秒数的个位。
GetLocalTime(st);
nd0.hIcon := hs[st.wSecond div 10];
nd1.hIcon := hs[st.wSecond mod 10];
//修改任务栏状态区的图标
Shell_NotifyIcon(NIM_MODIFY, @nd0);
Shell_NotifyIcon(NIM_MODIFY, @nd1);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
//将图标从任务栏状态区删除
Shell_NotifyIcon(NIM_DELETE, @nd0);
Shell_NotifyIcon(NIM_DELETE, @nd1);
end;
//处理 通知消息
procedure TForm1.WndProc(var Msg: TMessage);
var
IconID:integer;
pt:TPOINT;
begin
if msg.Msg = WM_TRAYNOTIFY then
begin
{
在通知消息中,wParam参数为图标的uID,
lParam参数为鼠标事件的类型。
}
iconID := msg.WParam;
//获取鼠标的在屏幕上的位置
GetCursorPos(pt);
//通知消息的处理的基本框架结构如下:
case msg.lParam of
WM_LBUTTONDOWN:
begin
//鼠标右键被按下
end;
WM_RBUTTONDOWN:
begin
//鼠标左键被按下
end;
WM_LBUTTONUP:
begin
//释放鼠标左键
end;
WM_RBUTTONUP:
begin
//释放鼠标右键
end;
WM_MOUSEMOVE:
begin
//鼠标在图标上移动
end;
WM_LBUTTONDBLCLK:
begin
//鼠标左键双击
end;
WM_RBUTTONDBLCLK:
begin
//鼠标右键双击
end;
end; //end case
end
else//调用父类的WndProc方法处理其它消息
inherited;
end;
Shell_NotifyIcon(NIM_MODIFY, @IconData);
//下面的代码实际上没有用,去掉也可以(至少目前这样认为)
IconData.DUMMYUNIONNAME.uVersion := NOTIFYICON_VERSION;
if not Shell_NotifyIcon(NIM_SETVERSION, @IconData) then
showmessage('setversion fail');
end;
别忘了删除过程:
procedure DeleteSysTrayIcon;
begin
DeallocateHWnd(IconData.Wnd);
if not Shell_NotifyIcon(NIM_DELETE, @IconData) then
showmessage('delete fail');
end;