unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,shellapi, Menus; const mousemsg = wm_user + 1; //自定义消息,用于处理用户在图标上点击鼠标的事件 iid = 100; //用户自定义数值,在TnotifyIconDataA类型全局变量ntida中使用 type TForm1 = class(TForm) PopupMenu1: TPopupMenu; exit1: TMenuItem; procedure exit1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } //自定义消息处理函数,处理鼠标点击图标事件 procedure mousemessage(var message: tmessage); message mousemsg; public { Public declarations } end;var Form1: TForm1; ntida: TNotifyIcondataA; //用于增加和删除系统状态图标 implementation{$R *.dfm} procedure TForm1.mousemessage(var message: tmessage); var mousept: TPoint; //鼠标点击位置 begin inherited; if message.LParam = wm_rbuttonup then begin //用鼠标右键点击图标 getcursorpos(mousept); //获取光标位置 popupmenu1.popup(mousept.x, mousept.y); //在光标位置弹出菜单 end; if message.LParam = wm_lbuttonup then begin //用鼠标左键点击图标 //显示应用程序窗口 ShowWindow(Handle, SW_SHOW); //在任务栏上显示应用程序窗口 ShowWindow(Application.handle, SW_SHOW); SetWindowLong(Application.Handle, GWL_EXSTYLE, not (GetWindowLong(Application.handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW)); end; message.Result := 0; end ; procedure TForm1.Exit1Click(Sender: TObject); begin //为ntida赋值,指定各项参数 ntida.cbSize := sizeof(tnotifyicondataa); ntida.wnd := handle; ntida.uID := iid; ntida.uFlags := nif_icon + nif_tip + nif_message; ntida.uCallbackMessage := mousemsg; ntida.hIcon := Application.Icon.handle; ntida.szTip := 'Icon'; shell_notifyicona(NIM_DELETE, @ntida); //删除已有的应用程序图标 Application.Terminate; //中断应用程序运行,退出应用程序 end;procedure TForm1.FormCreate(Sender: TObject); begin ntida.cbSize := sizeof(tnotifyicondataa); //指定ntida的长度 ntida.Wnd := handle; //取应用程序主窗体的句柄 ntida.uID := iid; //用户自定义的一个数值,在uCallbackMessage参数指定的消息中使用 ntida.uFlags := nif_icon + nif_tip + nif_message;//指定在该结构中uCallbackMessage、hIcon、szTip参数都有效 ntida.uCallbackMessage := mousemsg;//指定的窗口消息 ntida.hIcon := Application.Icon.handle;//指定系统状态栏显示应用程序的图标句柄 ntida.szTip := 'Icon'; //当鼠标停留在系统状态栏该图标上时,出现该提示信息 shell_notifyicona(NIM_ADD, @ntida); //在系统状态栏增加一个新图标 end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin Action := caNone; //不对窗体进行任何操作 ShowWindow(Handle, SW_HIDE); //隐藏主窗体 //隐藏应用程序窗口在任务栏上的显示 ShowWindow(Application.Handle, SW_HIDE); SetWindowLong(Application.Handle, GWL_EXSTYLE, GetWindowLong(Application.handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW); end; end.
其实用控件来的是最快的。也比较方便。可以选的东西也比较多。试试CoolTray
那用这个: Procedure TForm1.IconClick(var Msg:TMessage); var p:TPoint; begin if ((Msg.lParam=WM_LBUTTONDOWN) and (Status=True)) then Form2.Show; if (Msg.lParam=WM_RBUTTONDOWN) then begin GetCursorPos(P); PopupMenu1.Popup(p.x,p.y); end; end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellapi, Menus;
const
mousemsg = wm_user + 1; //自定义消息,用于处理用户在图标上点击鼠标的事件
iid = 100; //用户自定义数值,在TnotifyIconDataA类型全局变量ntida中使用
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
exit1: TMenuItem;
procedure exit1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
//自定义消息处理函数,处理鼠标点击图标事件
procedure mousemessage(var message: tmessage);
message mousemsg;
public
{ Public declarations }
end;var
Form1: TForm1;
ntida: TNotifyIcondataA; //用于增加和删除系统状态图标
implementation{$R *.dfm}
procedure TForm1.mousemessage(var message: tmessage);
var
mousept: TPoint; //鼠标点击位置
begin
inherited;
if message.LParam = wm_rbuttonup then begin //用鼠标右键点击图标
getcursorpos(mousept); //获取光标位置
popupmenu1.popup(mousept.x, mousept.y); //在光标位置弹出菜单
end;
if message.LParam = wm_lbuttonup then begin //用鼠标左键点击图标
//显示应用程序窗口
ShowWindow(Handle, SW_SHOW);
//在任务栏上显示应用程序窗口
ShowWindow(Application.handle, SW_SHOW);
SetWindowLong(Application.Handle, GWL_EXSTYLE,
not (GetWindowLong(Application.handle, GWL_EXSTYLE)
or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW));
end;
message.Result := 0;
end ;
procedure TForm1.Exit1Click(Sender: TObject);
begin
//为ntida赋值,指定各项参数
ntida.cbSize := sizeof(tnotifyicondataa);
ntida.wnd := handle;
ntida.uID := iid;
ntida.uFlags := nif_icon + nif_tip + nif_message;
ntida.uCallbackMessage := mousemsg;
ntida.hIcon := Application.Icon.handle;
ntida.szTip := 'Icon';
shell_notifyicona(NIM_DELETE, @ntida); //删除已有的应用程序图标
Application.Terminate; //中断应用程序运行,退出应用程序
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ntida.cbSize := sizeof(tnotifyicondataa); //指定ntida的长度
ntida.Wnd := handle; //取应用程序主窗体的句柄
ntida.uID := iid; //用户自定义的一个数值,在uCallbackMessage参数指定的消息中使用
ntida.uFlags := nif_icon + nif_tip + nif_message;//指定在该结构中uCallbackMessage、hIcon、szTip参数都有效
ntida.uCallbackMessage := mousemsg;//指定的窗口消息
ntida.hIcon := Application.Icon.handle;//指定系统状态栏显示应用程序的图标句柄
ntida.szTip := 'Icon'; //当鼠标停留在系统状态栏该图标上时,出现该提示信息
shell_notifyicona(NIM_ADD, @ntida); //在系统状态栏增加一个新图标
end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone; //不对窗体进行任何操作
ShowWindow(Handle, SW_HIDE); //隐藏主窗体
//隐藏应用程序窗口在任务栏上的显示
ShowWindow(Application.Handle, SW_HIDE);
SetWindowLong(Application.Handle, GWL_EXSTYLE,
GetWindowLong(Application.handle, GWL_EXSTYLE)
or WS_EX_TOOLWINDOW AND NOT WS_EX_APPWINDOW);
end;
end.
Procedure TForm1.IconClick(var Msg:TMessage);
var
p:TPoint;
begin
if ((Msg.lParam=WM_LBUTTONDOWN) and (Status=True)) then
Form2.Show;
if (Msg.lParam=WM_RBUTTONDOWN) then
begin
GetCursorPos(P);
PopupMenu1.Popup(p.x,p.y);
end;
end;
建立托盘图标需要建立一个窗体,此窗体可以是可见的也可以是不可见的,一般都采用创建不可见的窗体
使用classes单元的Allocatehwnd函数参数的意义是此窗体的消息回调函数,返回一个窗体句柄,
使用Shell_NotifyIcon来设置托盘属性,第一个参数为你的操作参数可以为以下三值之一:
NIM_ADD 增加一个图标
NIM_DELETE 删除一个图标
NIM_MODIFY 修改一个图标
第二个为一个指针,是指向一个TNotifyIconData类型的,此记录体中:Wnd代表托盘窗体的句柄,uflag为TNotifyIconData哪些字段是有效的可以是以下的组合:NIF_ICON hicon有效,NIF_MESSAGE uCallbackMessage有效,NIF_TIP szTip有效
uCallbackMessage代表消息的代号,可在消息回凋函数参数Message中得到,
hIcon图标句柄,szTip托盘的提示信息
最后,用classes单元的DellocateHWND清除窗体参数为窗体句柄