unit f_shbx;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, shellapi,
  Menus;const WM_NOTIFYICON = WM_USER + 1;
type
  Tserverfrom = class(TForm)
    PopupMenu1: TPopupMenu;
    about1: TMenuItem;
    exit1: TMenuItem;
    procedure exit1Click(Sender: TObject);
    procedure about1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    Notifydata: Tnotifyicondata;
    procedure addicon;
    procedure removeicon;
    { Private declarations }
  protected
    procedure onnotifyicon(var message: TMessage); message WM_NOTIFYICON;
  public
    { Public declarations }
  end;
var
  serverfrom: Tserverfrom;implementation{$R *.DFM}procedure Tserverfrom.addicon;
begin
  notifydata.cbSize := sizeof(notifydata);
  with notifydata do
  begin
    wnd := handle;
    uid := 1;
    uflags := NIF_TIP or NIF_ICON or NIF_MESSAGE;
    hicon := application.Icon.Handle;
    sztip := '应用服务器';
    ucallbackmessage := WM_NOTIFYICON;
  end;
  shell_notifyicon(NIM_ADD, @notifydata);
end;procedure Tserverfrom.removeicon;
begin
  notifydata.uID := 1;
  shell_notifyicon(NIM_DELETE, @notifydata);
end;procedure Tserverfrom.onnotifyicon(var message: Tmessage);
var
  mousepos: Tpoint;
begin
  if message.lparam = WM_LBUTTONDBLCLK then
    showmessage('不要乱动!')
  else
    if (message.lparam = WM_RBUTTONDBLCLK) then
    begin
      getcursorpos(mousepos);
      setforegroundwindow(application.handle);
      application.ProcessMessages;
      popupmenu1.Popup(mousepos.x, mousepos.y);
    end;
end;procedure Tserverfrom.exit1Click(Sender: TObject);
begin
  removeicon;
  close;
end;procedure Tserverfrom.about1Click(Sender: TObject);
begin
  showmessage('业务应用服务器');
end;procedure Tserverfrom.FormCreate(Sender: TObject);
begin
  addicon;
  showwindow(application.handle, SW_HIDE);
  Application.ShowMainForm := False;
end;procedure Tserverfrom.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  removeicon;
end;end.

解决方案 »

  1.   

    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:= 'taxi';
      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;