如题

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,shellapi, StdCtrls;
    const
      MY_MESSAGE = wm_user+100;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button1Click(Sender: TObject);
      private
        procedure OnIconNotify(var Message: TMessage);message MY_MESSAGE;  public
        { Public declarations }
      end;var
      Form1: TForm1;
    procedure AddSystray(hnd:HWnd);
    procedure DelSystray(hnd:HWnd);
    implementation{$R *.dfm}
    procedure AddSystray(hnd:HWnd);
    var
      nid: TNotifyIconData;
    begin
      nid.cbSize := sizeof(nid); // nid变量的字节数
      nid.Wnd := hnd; // 主窗口句柄
      nid.uID := 11; // 内部标识,可设为任意数
      nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指?
      nid.hIcon := Application.Icon.Handle; // 要加入的图标句柄,可任意指?
      nid.szTip := 'This is a test application'; // 提示字符串
      nid.uCallbackMessage := MY_MESSAGE; // 回调函数消息
      nid.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE; // 指明哪些字段有?
      if not Shell_NotifyIcon(NIM_ADD, @nid) then
      begin
        ShowMessage('Add systray Failed!');
      end;
    end;procedure DelSystray(hnd:HWnd);
    var
      nid: TNotifyIconData;
    begin
      nid.cbSize := sizeof(nid); // nid变量的字节数
      nid.cbSize := sizeof(nid); // nid变量的字节数
      nid.uID := 11; //内部标识,与加入小图标时的数一致
      nid.Wnd := hnd; //主窗口句柄
      Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
      Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标
    end;{ TForm1 }procedure TForm1.OnIconNotify(var Message: TMessage);
    var
      Busy: Boolean;
    begin
      Busy:=False; 
      if not Busy then
      begin
        Busy := true;
        if Message.LParam=WM_LBUTTONDOWN then
        begin
          if Application.MessageBox('Are you sure','Exit', MB_YESNO)=IDYES then
            show;
        end;
      end;
      Busy := false;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      AddSystray(handle);
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      DelSystray(handle);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      hide;
    end;end.
      

  2.   

    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,ShellAPI, StdCtrls, Menus;
    const WM_NID=WM_USER+1000;
    type
        TForm2 = class(TForm)
        PopupMenu1: TPopupMenu;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        NotifyIcon:TNotifyIconData;
        procedure WMNID(var msg:TMessage);message WM_NID;
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}procedure TForm2.FormCreate(Sender: TObject);
    Begin
      //NotifyIcon为全局变量,在程序的开头已经定义了 
      with NotifyIcon do
      begin
      cbSize:=SizeOf(TNotifyIconData);
      Wnd:=Handle;   //指向当前窗体Form1的句柄 
      uID:=1;
     uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
      uCallBackMessage:=WM_NID;
      hIcon:=Application.Icon.Handle;
      szTip:='张家恶少';
    end;
    //把设置好的变量NotifyIcon加入到系统中以便处理 
      Shell_NotifyIcon(NIM_ADD,@NotifyIcon);End;
    procedure tform2.WMNID(var msg:TMessage);
    begin
      case msg.LParam of
           WM_LBUTTONUp:Form2.Visible:=not Form2.Visible;
           //WM_RBUTTONUP: ShowMessage('您点击的是右键');
           WM_RBUTTONUP:
           begin
             PopupMenu1.Popup(mouse.CursorPos.X,mouse.CursorPos.Y);
           end;
      End;
    End;
    end.
      

  3.   

    如果你用RX系统控件的话就很简单
    原来我用系统API作,但结果却不如用RxTrayIcon感觉效果好
    而且它也有例子,很快就可以搞定
      

  4.   

    比如RX的RXtrayIcon就可以轻松搞定,而且支持鼠标事件