unit Main;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, AppEvnts, XPMenu;const
  WM_NotifyDataFromSA=$01000;type
  TMainForm = class(TForm)
    Pp: TPopupMenu;
    Close1: TMenuItem;
    ShowWindowMenu: TMenuItem;
    app: TApplicationEvents;
    MainMenu: TMainMenu;
    File1: TMenuItem;
    Exit1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Close1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure ShowWindowMenuClick(Sender: TObject);
    procedure appMessage(var Msg: tagMSG; var Handled: Boolean);
    procedure appMinimize(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure PpPopup(Sender: TObject);
  private
    { Private declarations }
    procedure WMNotifyDataFromSA( var Msg:TMessage);message WM_NotifyDataFromSA;
  public
    { Public declarations }
  end;var
  MainForm: TMainForm;implementationuses ShellAPI;
{$R *.DFM}
{$R Icon.res}
var
CanExit:Boolean=False;procedure TMainForm.WMNotifyDataFromSA( Var Msg:TMessage);
var
Pt:TPoint;
begin
if not IsWindowEnabled(Handle) then
  Exit;
  //if Msg.WParam=1981 then
    if Msg.LParam=WM_LBUTTONUP then
      begin
        ShowWindowMenu.Click;
      end
    else if Msg.LParam=WM_RBUTTONUP then
      begin;
      GetCursorPos(Pt);
      MainForm.BringToFront;
      Pp.Popup(Pt.x,Pt.y);
      end;
end;procedure TMainForm.FormCreate(Sender: TObject);
var
NotifyIcon:NotifyIconDataA;                           //定义NotifyIconDataA结构
Icon1:TIcon;
begin
   Icon1:=TIcon.Create;
   try
   Icon1.Handle:=LoadIcon(hInstance,'IconData');
   NotifyIcon.cbSize:=SizeOf(NotifyIconDataA);       //获取NotifyIcanDataA结构的大小
   NotifyIcon.Wnd:=MainForm.Handle;                     //收到状态区间图标相联系的通知消息的窗口句柄
   NotifyIcon.uID:=1981;                              //定义状态区间图标的标识符
   NotifyIcon.uFlags:=Nif_Icon+Nif_Message+Nif_Tip;  //定义了hIcon & uCallBackMessage & szTip合法
   NotifyIcon.uCallbackMessage:=WM_NotifyDataFromSA;    //定义了图标消息标识符
   NotifyIcon.hIcon:=Icon1.Handle;                   //定义了要处理的图标句柄
   finally
   Icon1.Free;
   end;
   NotifyIcon.szTip:='后台隐藏程序示例';             //显示的Tooltip文本
   Shell_NotifyIcon(NIM_Add,@NotifyIcon);
end;procedure TMainForm.FormDestroy(Sender: TObject);
var
NotifyIcon:NotifyIconDataA;
begin
   NotifyIcon.Wnd:=MainForm.Handle;
   NotifyIcon.uID:=1981;
   Shell_NotifyIcon(Nim_Delete,@NotifyIcon);
   //删除定义的图标
end;procedure TMainForm.Close1Click(Sender: TObject);
begin
  Close;
end;procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if not MainForm.Visible then
  case MessageBox(MainForm.Handle,'Are you sure to close this program?','Question',MB_YESNO+MB_ICONQUESTION) of
    idYes:CanClose:=True;
    idNo:CanClose:=False;
  end
else if not CanExit then
  begin
    Application.Minimize;
    CanClose:=False;
  end;
end;procedure TMainForm.ShowWindowMenuClick(Sender: TObject);
begin
  MainForm.Visible:=True;
  Application.Restore;
  Application.BringToFront;
end;
procedure TMainForm.appMessage(var Msg: tagMSG; var Handled: Boolean);
begin
  //if Msg.message=SW_SYSCOMMAND then
end;procedure TMainForm.appMinimize(Sender: TObject);
begin
  MainForm.Visible:=False;
end;
procedure TMainForm.Exit1Click(Sender: TObject);
begin
  CanExit:=TRue;
  Close;
end;procedure TMainForm.PpPopup(Sender: TObject);
begin
   SetForegroundWindow(Application.Handle);  
   SetForegroundWindow(MainForm.Handle);
end;end.