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;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);
private
procedure OnIconNotify(var Message: TMessage);
message MY_MESSAGE;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{当小图标捕捉到鼠标事件时进入此过程}
{当小图标捕捉到鼠标事件时进入此过程}
procedure TForm1.OnIconNotify(var Message: TMessage);
const
Busy: Boolean = false;
begin
if not Busy then begin
Busy := true;
if Message.LParam=WM_LBUTTONDOWN then
if Application.MessageBox('Are you sure',
'Exit', MB_YESNO)=IDYES then Close;
Busy := false;
end;
end;
nid.szTip := 'This is a test application'; // 提示字符串
nid.uCallbackMessage := MY_MESSAGE; // 回调函数消息
nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有?
if not Shell_NotifyIcon(NIM_ADD, @nid) then begin
ShowMessage('Failed!');
Application.Terminate;
end;
{将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
end;