var
  FpNotify : PNotifyIconDataA;New(FpNotify);
with FpNotify^ do
begin
Wnd := Handle;
uID := 0;
uFlags := NIF_ICON + NIF_MESSAGE + NIF_TIP;
hIcon := Icon.Handle;
uCallbackMessage := c_nTaskIcoMsg;
szTip := 'My program';
end;
Shell_NotifyIcon(NIM_ADD, FpNotify);

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,ShellAPI, Menus;
    const
      WM_TRAYICON=WM_APP+0;
    type
      TForm1 = class(TForm)
        btnadd: TButton;
        btndelete: TButton;
        tryMenu: TPopupMenu;
        Hello1: TMenuItem;
        Exit1: TMenuItem;
        procedure btnaddClick(Sender: TObject);
        procedure WMTTRAYICON(var message:Tmessage);message WM_TRAYICON;
        procedure FormCreate(Sender: TObject);
        procedure btndeleteClick(Sender: TObject);
        procedure Exit1Click(Sender: TObject);
        procedure Hello1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      Nidata:TNotifyIconData;
    implementation{$R *.dfm}procedure TForm1.btnaddClick(Sender: TObject);begin  Shell_NotifyIcon(NIM_ADD,@Nidata);
    end;procedure Tform1.WMTTRAYICON(var message:Tmessage);
    var
      MousePos:TPoint;
    begin
      if message.LParam=WM_RBUTTONDOWN then
      begin
        setactiveWindow(form1.Handle);
        Getcursorpos(MousePos);
        TryMenu.Popup(MousePos.X,MousePos.Y);
      end;
      if message.LParam=WM_LBUTTONDBLCLK then
      begin
        setactiveWindow(form1.Handle);
        messagebox(form1.Handle,'hello','hello',0);
      end;
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      with Nidata do
      begin
        cbsize:=sizeof(TNotifyIconData);
        uID:=0;
        uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
        wnd:=form1.Handle;
        uCallbackMessage:=WM_TRAYICON;
        hIcon:=Application.Icon.Handle;
        StrPCopy(szTip,'hello!');
      end;
    end;procedure TForm1.btndeleteClick(Sender: TObject);
    begin
      Shell_NotifyIcon(NIM_DELETE,@Nidata);
    end;procedure TForm1.Exit1Click(Sender: TObject);
    begin
      application.Terminate;
    end;procedure TForm1.Hello1Click(Sender: TObject);
    begin
      messagebox(form1.Handle,'successful!','warning',2);
    end;end.
      

  2.   

    const
        WM_TrayMessage=WM_User+100;  protected
          procedure AppMinimized(Sender: TObject);
          procedure WMTrayMessage(var msg:TMessage);message WM_TrayMessage;uses ShellApivar
      NID:TNotifyIconData{点击了任务栏的图标}
    procedure TMainForm.WMTrayMessage(var msg:TMessage);
    var
      p:TPoint;
    begin
      if msg.LParam=WM_LButtonDown then
      begin
        ShowWindow(Application.Handle,SW_Show);
        Application.Restore;
      end
      else if msg.LParam=WM_RButtonDown then
      begin
        GetCursorPos(p);
        pmTray.Popup(p.x,p.y);
      end;
    end;{应用程序最小化时图标显示在任务栏}
    procedure TMainForm.AppMinimized(Sender:TObject);
    begin
      NID.cbSize:=SizeOf(TNotifyIconData);
      NID.hIcon:=Application.Icon.Handle;
      NID.szTip:= '彩虹2.0 Beta ';
      NID.uCallbackMessage:=WM_TrayMessage;
      NID.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
      NID.uID:=0;
      NID.Wnd:=Handle;
      Shell_NotifyIcon(NIM_ADD,@NID);
      ShowWindow(Application.Handle,SW_Hide);
    end;procedure TMainForm.FormCreate(Sender: TObject);
    begin
      Application.OnMinimize:=AppMinimized;
      AppMinimized(nil);
    end;procedure TMainForm.FormDestroy(Sender: TObject);
    begin
      Shell_NotifyIcon(NIM_DELETE,@NID);
    end;