使用第三方控件TRAYICON吧,它可以帮你解决问题。

解决方案 »

  1.   

    我想自己编写代码呀,因为别的Tray方面的问题我都已解决,只剩下菜单的问题了。
    难道没人知道吗?
      

  2.   


    请关注:注释部分
    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.
      

  3.   

    kuangning(郎之间,穷人),你的代码也太长了吧,好复杂!~
    我自己已经搞定了,用了很简单的方法,是从以前的帖子中搜出来的。
    SetForegroundWindow(Application.Handle);
    Application.ProcessMessages;
    PopupMenu1.Popup...就可以了。
    多谢各位参与啊!
    不过,现在不知道怎么给分了,呵呵。