用trayicon控件实现
或找一个这样的单元文件

解决方案 »

  1.   

    给你看个样例:unit main;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Grids, Menus, Outline, DirOutln, FileCtrl;
    const
      MyIcoMes=WM_USER+78;
    type
      Tfrmmain = class(TForm)
        PopupMenu1: TPopupMenu;
        Nvisible: TMenuItem;
        Nclose: TMenuItem;
        procedure FormCreate(Sender: TObject);
        procedure NcloseClick(Sender: TObject);
        procedure NvisibleClick(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
        procedure AddIcon();
      public
        { Public declarations }
        procedure MyIcoMesDo(var Msg : TMessage);Message MyIcoMes; //托盘图标的返回消息
        procedure WMSyscommand(var msg: TWMSyscommand);message Wm_syscommand;
      end;
    var
      frmmain: Tfrmmain;
    implementation
    uses
      shellapi,syncobjs;
    {$R *.DFM}
    var
      PNotify: PNotifyIconDataA;
    procedure tfrmMain.AddIcon();
    begin
      New(PNotify);
      PNotify^.Wnd:=self.Handle;
      PNotify^.uID:=0;
      PNotify^.uFlags:=NIF_ICON+NIF_MESSAGE+NIF_TIP;
      PNotify^.hIcon:=self.Icon.Handle;
      PNotify^.uCallbackMessage:=MyIcoMes;
      PNotify^.szTip:='我的托盘程序';
      Shell_NotifyIcon(NIM_ADD,PNotify);
      self.Visible:=false;
    end;procedure Tfrmmain.FormCreate(Sender: TObject);
    begin
      AddIcon;
    end;
    procedure tfrmMain.MyIcoMesDo(var Msg:TMessage);
    begin
      if (msg.LParam=wm_rbuttonDown) and (not self.Visible) then
          popupMenu1.Popup(screen.Width-15 ,screen.Height-80);  //在图标上单击右键,弹出菜单
      if (Msg.LParam=WM_LBUTTONDBLCLK) and self.Enabled then
        NvisibleClick(self);//双击图标,显示(隐藏)窗口
      if (Msg.LParam=WM_lbuttonDown) and self.Enabled then
        NvisibleClick(self);//单击图标,显示(隐藏)窗口
    end;
    procedure TfrmMain.WMSyscommand(var msg: TWMSyscommand);
    begin
     case   msg.CmdType of
       SC_MINIMIZE: NvisibleClick(self); //窗口系统命令,最小化时隐藏窗口
       sc_close:close;      //关闭命令,关闭窗口
     end;
    end;
    procedure Tfrmmain.NcloseClick(Sender: TObject);
    begin
      close;
    end;
    procedure Tfrmmain.NvisibleClick(Sender: TObject);
    begin
      if copy(Nvisible.Caption,1,4)='隐藏' then
        Nvisible.Caption:='显示'
      else nvisible.caption:='隐藏';
      self.Visible:=not self.Visible;
    end;
    procedure Tfrmmain.FormDestroy(Sender: TObject);
    begin
      Shell_NotifyIcon(NIM_delete,PNotify);
      Dispose(Pnotify);
    end;
    end.下面是窗口上放的一个POPUPMENU。
      object PopupMenu1: TPopupMenu
        Left = 120
        Top = 128
        object Nvisible: TMenuItem
          Caption = '隐藏'
          OnClick = NvisibleClick
        end
        object Nclose: TMenuItem
          Caption = '退出'
          OnClick = NcloseClick
        end
      end