procedure TForm1.TrayShow(Sender: TObject);
//当点击托盘图标时显示或隐含主窗体
begin
PopupMenu1.AutoPopup:=False;
//设定 TNotifyIconData 的记录长度
MyTrayIcon.cbSize :=SizeOf(tnotifyicondata);
//确定调用程序的窗体句柄
MyTrayIcon.Wnd :=Handle;
//确定图标的 uID
MyTrayIcon.uID :=1; ////???我不知道这应该是多少!!!
//设定显示标记
MyTrayIcon.uFlags :=NIF_ICON or NIF_TIP or NIF_MESSAGE;
//用户自定义消息
MyTrayIcon.uCallbackMessage := WM_MYTRAYICONCALLBACK;
//托盘图标的句柄
//MyTrayIcon.hIcon := Application.Icon.Handle;
MyTrayIcon.hIcon := LoadIcon(0, IDI_WINLOGO);
//托盘图标的提示信息
MyTrayIcon.szTip :='定时导数据程序';
//向托盘中添加图标
Shell_NotifyIcon(NIM_ADD,@mytrayicon);
end;
procedure TForm1.WMMyTrayIconCallBack(var Msg: TMessage);
//处理点击托盘图标的事件,根据 WM_MOUSEMOVE 消息的不同情况产生不同的回应
var
CursorPos : TPoint;
begin
PopupMenu1.AutoPopup:=False;
case Msg.lParam of
//左键按下
WM_LBUTTONDOWN : begin
application.MainForm.BringToFront; //窗体置前
end;
//左键双击
WM_LBUTTONDBLCLK : begin //窗体隐含或显示
Application.MainForm.Visible := not Application.MainForm.Visible;
SetForegroundWindow(Application.Handle);
end;
//右键按下
WM_RBUTTONDOWN : begin //显示弹出菜单
GetCursorPos(CursorPos);
PopupMenu1.Popup(CursorPos.x,CursorPos.y);
end;
end//case
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);//删除托盘图标
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);//程序不在任务栏上显示
TrayShow(Application.MainForm);//显示图标
flag:=0;
end;
procedure TForm1.n_MainFromDispOrHideClick(Sender: TObject);
begin
if Application.MainForm.Visible then
begin
Application.MainForm.Hide;
end
else
begin
Application.MainForm.Show;
n_MainFromDispOrHide.Caption :='隐藏主窗体';
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
ic:TICON;
begin
if flag=0 then flag:=1
else flag:=0;
ic:=TICON.Create;
ImageList1.GetIcon(flag,ic);
with MyTrayIcon do
begin
cbSize := SizeOf(tnotifyicondata);
uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;
uID := 0; ///该是多少啊?
Wnd := Handle;
uCallbackMessage := WM_MYTRAYICONCALLBACK;
// hIcon :=Application.Icon.Handle ;]
hicon:=ic.Handle;
end;
Shell_NotifyIcon(NIM_MODIFY,@MyTrayIcon); ///没有任何效果!
Application.MainForm.Hide;
ic.Free;
end;
end.