我刚用过,你看看下面的代码:<br>unit Mainfrm;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls,Registry,ShellAPI, Menus; //加上ShellApi<br>const<br> MYTRAY_MESSAGE=WM_USER+100; //托盘图标返回消息<br>type<br> Tmainform = class(TForm)<br> TrayPopMenu: TPopupMenu;<br> POPSet: TMenuItem;<br> POPEXIT: TMenuItem;<br> procedure FormCreate(Sender: TObject);<br> procedure POPEXITClick(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure FormPaint(Sender: TObject);<br> procedure POPSetClick(Sender: TObject);<br> private<br> { Private declarations }<br> TrayIconData : TNotifyIconData; //定义托盘图标结构<br> //托盘消息处理事件<br> procedure MyTrayMessage(var message:TMessage);message MyTRAY_MESSAGE;<br><br> public<br> { Public declarations }<br> tag:boolean;<br> end;<br><br>var<br> mainform: Tmainform;<br><br>implementation<br><br>uses Setfrm, UData;<br><br>{$R *.DFM}<br><br>//---------------------------------------------------------------------------<br>//---------使系统启动时,程序自动运行<br>//---------------------------------------------------------------------------<br><br>procedure Tmainform.FormCreate(Sender: TObject);<br>Var //使系统启动时,程序自动运行<br> RegF:TRegistry;<br>begin<br> tag:=true;<br> RegF:=TRegistry.Create;<br> RegF.RootKey:=HKEY_LOCAL_MACHINE;<br> Try<br> RegF.OpenKey('SOFTWARE/Microsoft/Windows/CurrentVersion/Run',true);<br> //这一句判断是否为第一次运行<br> if not Regf.valueExists('GDHclient') then<br> begin<br> tag:=false;<br> RegF.WriteString('GDHclient','"' + ParamStr(0) + '"');<br> Application.MessageBox('谢谢您使用本程序,第一次使用请您先进行初始设置!','提示信息',MB_OK+MB_ICONWARNING);<br> end;<br> except<br> End;<br> RegF.CloseKey;<br> RegF.Free;<br> //建立托盘图标<br> TrayIconData.cbSize := sizeof(TrayIconData); //TrayIconData变量的字节数<br> TrayIconData.Wnd := Handle; //主窗口句柄<br> TrayIconData.uID := 1; //内部标识,可设为任意数<br> TrayIconData.hIcon := Icon.Handle;<br> {要加入的图标句柄,可任意指定,此处为在Tform中Icon属性中指定的图标<br> TrayIconData.hIcon := Application.Icon.Handle;<br> 此句使用了程序的图标,和上句程序有区别,下句亦可,但推荐使用上句,这样图标加<br> 入到托盘中时图标不会走样。}<br> TrayIconData.hIcon := LoadIcon(0,IDI_APPLICATION);<br> TrayIconData.szTip := '正在运行-//你的程序';//图标的提示信息,即黄色的Hint<br> TrayIconData.uCallbackMessage := MYTRAY_MESSAGE;<br> TrayIconData.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; //指明哪些字段有效<br> {将程序的窗口样式设为TOOL窗口,可避免在任务条上出现}<br> SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);<br> Shell_NotifyIcon(NIM_ADD, @TrayIconData); //向托盘中添加图标<br>end;<br><br>//---------------------------------------------------------------------------<br>//------------退出程序<br><br>procedure Tmainform.POPEXITClick(Sender: TObject);<br>begin//弹出菜单事件<br> close;<br>end;<br><br>//---------------------------------------------------------------------------<br>//-------------当关闭程序时<br><br>procedure Tmainform.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> //TrayIconData.cbSize := sizeof(TrayIconData); //TrayIconData变量的字节数<br> //TrayIconData.uID := 1; //内部标识,与加入小图标时的数一致<br> //TrayIconData.Wnd := Handle; //主窗口句柄<br> //TrayIconData.uFlags := 0;<br> Shell_NotifyIcon(NIM_DELETE, @TrayIconData); //去掉小图标<br>end;<br><br>//---------------------------------------------------------------------------<br>//------------我也不知这是什么?<br>procedure Tmainform.FormPaint(Sender: TObject);<br>begin<br> Hide;<br>end;<br><br>//---------------------------------------------------------------------------<br>//-------消息处理事件<br>//---------------------------------------------------------------------------<br><br>procedure TMainForm.MyTrayMessage(var message: Tmessage);<br>var //托盘消息处理事件<br> CursorPos : TPoint;<br>begin<br> //在这里处理用户点击托盘图标事件,可以根据WM_MOUSEMOVE消息的不同情况产生不同的回应,例如区别对待单击和双击等等<br> case message.lParam of<br> WM_RBUTTONDOWN :<br> begin<br> GetCursorPos(CursorPos);<br> SetForegroundWindow(handle);<br> //此句作用是当程序失去焦点时,弹出菜单也随之消失。<br> Application.ProcessMessages;<br> //取得光标当前位置<br> TrayPopMenu.Popup( CursorPos.x ,CursorPos.y );<br> PostMessage(Application.MainForm.Handle, WM_NULL, 0, 0);<br> end;<br> end;<br><br>end; <br><br>//---------------------------------------------------------------------------<br>//-------创建并打开设置窗口<br>//---------------------------------------------------------------------------<br><br>procedure Tmainform.POPSetClick(Sender: TObject);<br>begin //创建并打开设置窗口,以便进行操作<br> setform:=Tsetform.create(application);<br> try<br> setform.showmodal;<br> finally<br> setform.free;<br> end; <br>end;<br><br>end.<br>另外,在工程文件中加入下面的:<br>program SysGDH;<br><br>uses<br> Forms,<br> Mainfrm in 'Mainfrm.pas' {mainform},<br> Setfrm in 'Setfrm.pas' {Setform},<br> UData in 'UData.pas' {Moduleform: TDataModule};<br><br>{$R *.RES}<br><br>begin<br> Application.Initialize;<br> Application.CreateForm(Tmainform, mainform);<br> Application.ShowMainForm := False; //使主窗体在面子上不闪动加上这一句<br> Application.Run;<br><br>end.