unit TrayIcon;interfaceUses Windows ,SysUtils ,Messages ,ShellAPI ,Classes,Graphics ,Forms ,Menus ,
      StdCtrls ,ExtCtrls ;
Type
     ENotifyIconError = class(Exception);
     TTrayNotifyIcon = class (TComponent )
     private
           FDefaultIcon : THandle ;
           FIcon : TIcon ;
           FHideTask : Boolean ;
           FHint : String ;
           FIconVisible : Boolean ;
           FPopupMenu : TPopupMenu ;
           FOnClick : TNotifyEvent ;
           FOnDblClick : TNotifyEvent ;
           FNoShowClick : Boolean ;
           FTimer : TTimer ;
           Tnd : TNotifyIconData ;
           Procedure SetIcon ( Value : TIcon );
           procedure SetHideTask(Value : Boolean );
           procedure setHint(Value : String );
           Procedure SetIconVisible(Value : Boolean );
           procedure setpopupMenu(value : TpopupMenu);
           Procedure SendTrayMessage(Msg : DWORD ;Flags : UINT);
           Function  ActiveIconHandle : Thandle ;
           Procedure OnButtonTimer(Sender : TObject );
     Protected
           procedure Loaded ;override ;
           Procedure LoadDefaultIcon;Virtual ;
           procedure Notification(AComponent :TComponent ;Operation :TOperation);override ;
     public
           constructor Create(AOwner :TComponent );override ;
           destructor  Destroy ;override ;
     published
           property Icon : TIcon read FIcon write SetIcon ;
           property HideTask: boolean read FhideTask write SetHideTask default false ;
           property Hint: string read Fhint write setHint ;
           property IconVisible : Boolean read FIconVisible write setIconVisible default false ;
           property Popupmenu :TPopupmenu Read FPopupmenu write setPopupmenu;
           property Onclick : TNotifyEvent read FOnclick write FOnclick;
           property OnDBlclick : TNotifyEvent read FOndblclick write FOndblclick;
     end;procedure Register;
implementation
    Type
     TIconManager = class
     Private
           FHWindow : HWnd ;
           Procedure TrayWndProc (Var Message :Tmessage);
     public
           constructor Create ;
           destructor Destroy ;override ;
           property    HWindow : HWnd Read FHWindow write FHWindow ;
     end;     var
         IconMgr : TIconManager ;
         DDGM_TRAYICON : Integer ;
constructor TIconManager.Create ;
begin
   FHWindow := ALLocateHWnd(TrayWndProc );
end ;destructor TIconManager.Destroy ;
begin
   if FHWindow <>0 then DeallocateHWnd(FHWindow);
   Inherited destroy ;
end ;
procedure TIconManager.TrayWndProc(var message : Tmessage);
var
 pt: Tpoint ;
 TheIcon : TTrayNotifyIcon ;
begin
   with message do
   begin
        if (Msg = DDGM_TRAYICON) then
        begin
          TheIcon := TTRayNotifyIcon (WParam);
          case lParam of
               WM_LBUTTONDOWN:TheIcon.FTimer.Enabled := true ;
               WM_LBUTTONDBLCLK :
               begin
                  theIcon.FNoShowClick := true ;
                  if Assigned(TheIcon.FOnDblClick) then TheIcon.FOnDblClick (self);
               end;
               WM_RBUTTONDOWN :
               begin
                   if Assigned(theiCON.FPopupMenu) THEN
                   begin
                      SetForeGroundWindow(IconMgr.hwindow);
                      GetCursorpos(pt);
                      TheIcon.FPopupMenu.Popup (pt.x,pt.y);
                      postMessage(IconMgr.Hwindow ,WM_USER,0,0);
                   end;
               end;
        end ;
      end else
       REsult := DefWindowProc(FHWindow,Msg,Wparam,lparam);   end ;
end ;
{TTrayNotifyIcon}
Constructor TTRayNotifyIcon.Create (Aowner : TComponent);
begin
   Inherited Create(AOwner);
   FIcon := TIcon.Create ;
   Ftimer := ttimer.Create (self );
   with Ftimer do
   begin
      Enabled := true ;
      Interval := GetDoubleClickTime ;
      Ontimer := OnButtonTimer ;
   end ;
   {keep Default window Icon Handy..}
   LoadDefaulticon ;
end;
Destructor TTrayNotifyIcon.Destroy ;
begin
   if FIconVisible then SetIconVisible(false );
   fIcon.Free ;
   Inherited Destroy ;
end ;
function TTrayNotifyIcon.ActiveIconHandle : Thandle ;
begin
  if ( Ficon.Handle <>0) then
     Result :=FIcon.Handle
   else
    Result := FDefaultIcon ;
end ;
Procedure TTrayNotifyIcon.LoadDefaultIcon ;
begin
  FDefaultIcon := loadIcon(0,IDI_WINLOGO);
end;
procedure TTrayNotifyIcon.Loaded ;
begin
   Inherited Loaded ;
   if Ficonvisible then
       sendTRaymessage(NIM_ADD,NIF_MESSAge OR NIF_Icon or NIF_TIP);
end ;
procedure TTrayNotifyIcon.Notification (AComponent :TComponent ;Operation :TOperation);
begin
    Inherited  Notification(AComponent,Operation );
    if (operation = OPRemove ) and (AComponent = PopupMenu ) then
     popupmenu := nil ;
end ;
procedure TTrayNotifyIcon.OnButtonTimer (sender : TObject );
begin
     fTimer.Enabled := false ;
     if (not FNoshowclick) AND assigned(FOnclick) then
        Fonclick (self);
     Fnoshowclick := false ;
end ;
procedure TTrayNotifyIcon.SendTrayMessage(Msg : DWORD ;Flags : UINT);
begin
   with Tnd do
   begin
       cbsize :=sizeof (tnd);
       StrPlcopy(szTip,pchar(Fhint),sizeof (sztip));
       UFlags := flags ;
       uID := UINT(self );
       Wnd:=Iconmgr.HWindow ;
       Ucallbackmessage:=DDGM_TRAYICON ;
       HICON := activeIconHandle ;
   end ;
   shell_Notifyicon(Msg,@Tnd);
end ;
procedure TTrayNotifyIcon.SetHideTask(Value : Boolean );
const showarray : array[boolean] of integer =(sw_showNormal,sw_hide);
begin
   if fhidetask <> value then
    begin
        fhidetask := value ;
        if not(csdesigning in componentstate) then
            showwindow(Application.handle,showArray[FHideTask]);
    end ;end ;
procedure TTrayNotifyIcon.setHint(Value : String );
begin
  if Fhint <>Value then
    begin
      Fhint := Value ;
      if FIconvisible then
         sendTrayMessage(NIM_MODIFY,NIF_TIP);
    end ;
end ;
procedure TTrayNotifyIcon.SetIcon(Value :  TIcon );
begin
  ficon.Assign (Value);
  if FIconvisible then sendTrayMessage(NIM_MODIFY,NIF_ICON);
end ;
procedure TTrayNotifyIcon.setpopupMenu(value : TpopupMenu);begin
   FPOPupmenu :=Value ;
   if Value <> nil then Value.FreeNotification (self );
end ;
procedure  TTrayNotifyIcon.SetIconVisible(Value : Boolean );
const
   MsgArray : Array [boolean] of DWORD = ( NIM_DELETE,NIM_ADD);
begin
   if ficonvisible<>Value then
    begin
      ficonvisible :=Value ;
      sendtraymessage(msgarray[Value],NIF_MESSAGE or NIF_ICON or NIF_TIP);
    end ;end ;
procedure Register;
begin
  RegisterComponents('KNTEMP', [TTrayNotifyIcon]);
end;
const TrayMsgStr ='DDG.TrayNotifyIconMsg';initialization
    DDGM_TRAYICON := RegisterWindowMessage(TrayMsgStr);
    IconMgr := TiconManager.Create ;
finalization
    Iconmgr.Free ;
end.

解决方案 »

  1.   

    Application.ShowMainForm:=false;
    //如果还不行
    //在主Form中的OnShow
    ShowWindows(Application.Handle,SW_HIDE);
      

  2.   

    kuangning(郎之间,穷人): 最小化时,不管HideTask属性是否为True,在任务栏中都会出现,在FormCreate中加入
    SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
    后,最小化时还是跟以前程序一样。好像程序是Desktop的子Form一样,最小化时出现在左下方。InsideDelphi(ID):在在主Form中的OnShow中加入
    ShowWindows(Application.Handle,SW_HIDE); 
    当我想Show出来时怎么办?