![]()
编写系统托盘程序
![]() 如果大家觉得使用Delphi本身默认的图标不能体现个性化的话,那么我们可以点击“Project”菜单下面的“Options”项,跳转到“Application”页面,点击“Load Icon”从外部引入一个新图标(*.ico)。 由于本程序使用到TNotifyIconData类型,所以必须在uses部分加入Shellapi单元。 2.程序的清单 unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Controls, Forms, shellapi,Menus, StdCtrls; const ICON_ID = 1; ICONEVENT = WM_USER + 1; type TForm1 = class(TForm) PopupMenu1: TPopupMenu; Resize: TMenuItem; Exit: TMenuItem; Button1: TButton; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ExitClick(Sender: TObject); procedure ResizeClick(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } procedure InstallIcon; procedure UnInstallIcon; procedure IconOnclick(var message: TMessage); message ICONEVENT; public { Public declarations } end; var Form1: TForm1; IconData: TNotifyIconData; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin InstallIcon; //安装图标 SetWindowLong(Application.Handle, GWL_EXSTYLE, W*_**_TOOLWINDOW); //在任务栏中隐藏程序 end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin UnInstallIcon; //卸载图标 end; procedure TForm1.InstallIcon; begin IconData.cbSize:=SizeOf(IconData); IconData.Wnd:=Handle; IconData.uID:=ICON_ID; IconData.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP; IconData.uCallBackMessage:=ICONEVENT; IconData.hIcon:=application.Icon.Handle;//获得程序图标 IconData.szTip:='任务栏图标'; //图标的提示信息 Shell_NotifyIcon(NIM_ADD, @IconData ); end; procedure TForm1.UnInstallIcon; begin //卸载图标 Shell_NotifyIcon(NIM_DELETE, @IconData ); end; //在图标上按下鼠标 procedure TForm1.IconOnclick(var message: TMessage); var p: TPoint; begin case message.LParam of WM_RBUTTONDOWN: //点击鼠标右键时响应 begin GetCursorPos(p); //在鼠标点击处弹出快捷菜单 form1.PopupMenu1.Popup(p.x ,p.y); end; WM_LBUTTONDOWN: //点击鼠标左键时响应 begin form1.WindowState:=wsNormal;//还原窗口大小 form1.Visible:=true; end; end; end; procedure TForm1.ExitClick(Sender: TObject); begin form1.Close; end; procedure TForm1.ResizeClick(Sender: TObject); begin form1.WindowState:=wsNormal; //还原窗口大小 form1.Visible:=true; end; procedure TForm1.Button1Click(Sender: TObject); begin form1.WindowState:=wsMinimized; //最小化窗口 form1.Visible:=false; end; end. 本程序在Win98+Delphi6,WinXP+Delphi6中均编译运行通过。 |
delphi编写系统托盘程序
最新推荐文章于 2022-02-21 11:32:04 发布