Delphi实现系统托盘图标

1、创建一个应用程序,在主窗体上增加一个TpopupMenu组件。并为该弹出菜单组件增加标题(Caption)为“退出”的一个选项。     
    
2、在Uses中添加ShellAPI,因为在系统状态栏中增加图标时需调用ShellAPI中的函数Shell_NotifyIconA。该函数需要两个参数,其中一个是TnotifyIconDataA结构,需在主窗体中增加TnotifyIconDataA类型的全局变量ntida。     
    
3、定义消息mousemsg,并编写主窗体的mousemessage消息处理函数,此函数说明在图标上用鼠标左键单击时,会打开应用程序窗口;用鼠标右键单击时,会弹出一个选单。     
    

下面给出步骤2和3的实现代码: 

unit Unit1;
interface
uses
  Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,
  StdCtrls,   ExtCtrls,   Menus,   shellapi;
const
  mousemsg   =   wm_user   +   1;   //自定义消息,用于处理用户在图标上点击鼠标的事件
  iid   =   100;   //用户自定义数值,在TnotifyIconDataA类型全局变量ntida中使用
type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    {.......}
  private
    //自定义消息处理函数,处理鼠标点击图标事件
    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;

 4、编写FormCreate的代码如下:     

 

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   :=   '实现系统托盘图标!';   
  //当鼠标停留在系统状态栏该图标上时,出现该提示信息   
  shell_notifyicona(NIM_ADD,   @ntida);   
  //在系统状态栏增加一个新图标   
end;    


5、编写Tform1.OnClose的代码如下:     

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;     

   
6、编写“退出”代码如下:    
   
  当用户点击“退出”时实现完全退出应用程序。具体代码如下:     
    
procedure   TForm1.ExitClick(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;     

    
  通过以上步骤,我们即可用Delphi轻松实现系统状态栏托盘图标。 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
DELPHI7下好用的托盘控件,安装简单,版本进度如下:TCoolTrayIcon, ver. 2.3.0 - ver. 2.3.0: Various minor bugs fixed: 1) Calling the ShowMainForm and HideMainForm methods before the main form was created would crash the app. Fixed thanks to James Legg. 2) The tooltip would give trouble if it was 64 chars. Thanks to Toms Baugis and Teus de Jong. 3) The popup menu would not close itself auto- matically if the StartMinimized property was true. Thanks to Ingo Krueger, Chris Witt, and Reister HansJoerg. - ver. 2.2.2: When there was more than one form visible the taskbar icon would not hide when the main form was minimized. Fixed. - ver. 2.2.1: Fixed a popup menu bug. Also fixed a bug which meant D2 and D3 users could not compile the component. Added more icon animations in the demo. Thanks to Daniele Gervasoni for the "tray graph". - ver. 2.2.0: IconIndex is now a property, allowing you to specify an icon at run-time. The OnCycle event is changed so it returns the index to the next icon that will display when cycling. Finally, I fixed a bug that caused the taskbar icon not to display itself when MinimizeToTray was true. Thanks to Lorenz Graf for pointing it out to me. - ver. 2.1.4: The main form will no longer show itself untimely after the form's BorderStyle property is changed. Thanks to Thomas Reimann. - ver. 2.1.3: Fixed a bug that caused the main form not to display itself (how embarassing). - ver. 2.1.2: I *finally* found the bug that would cause some compound controls (like controls on notebook pages or tab sheets) not to display properly when StartMinimized was true. Incidently, this also means that TForm.Position now works. Also fixed a minor bug that caused modal forms owned by the main form to appear behind the main form when the popup menu appeared (thanks to Arash Ramin). - ver. 2.1.1: Added notification method to properly detect whether the associated popup menu and imagelist are deleted. Thanks to

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值