Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (19)


Starting an application as an icon in the system trayComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Example with full source code
Product:
Delphi 3.x (or higher)
Category:
Shell API
Skill Level:
Scoring:
Last Update:
04/23/2006
Search Keys:
delphi delphi3000 article borland vcl code-snippet start icon system tray task-bar popup pop-up menu run execute background show display hide hidden Shell_NotifyIcon TNotifyIconData
Times Scored:
40
Visits:
20604
Uploader: Ernesto De Spirito
Company: Latium Software
Reference: Latium Software's DELPHI TIPS
Component Download: http://www.latiumsoftware.com/download/d00035.zip
 
Question/Problem/Abstract:
How can I make my application start as an icon in the system tray?
Answer:



Placing an icon the system tray
-------------------------------

  NOTE: You can download the full source code of this article from
        http://www.latiumsoftware.com/download/d00035.zip


The system tray is the area at the right of the task bar where "background" applications can place their icons. To place an icon in the tray you have to call the Shell_NotifyIcon API declared in the ShellAPI unit, passing it the constant NIM_ADD (to tell it to add an icon) and the address of a TNotifyIconData record with the icon information that includes the handle of the icon to show, the text to show as tool tip when the mouse is over the icon, the handle of the window that will receive the messages of the icon and the message type the icon will send to this window.

  uses
     ..., ShellAPI;

  const
    WM_ICONTRAY = WM_USER + 1;   // User-defined message

  type
    ...

  var
    ...
    NotifyIconData : TNotifyIconData;

  implementation

  procedure TForm1.FormCreate(Sender: TObject);
  begin
    with NotifyIconData do begin
      hIcon := Icon.Handle;
      StrPCopy(szTip, Application.Title);
      Wnd := Handle;
      uCallbackMessage := WM_ICONTRAY;
      uID := 1;
      uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
      cbSize := sizeof(TNotifyIconData);
    end;
    Shell_NotifyIcon(NIM_ADD, @NotifyIconData);
  end;


Handling icon messages
----------------------

Form1 will receive WM_ICONTRAY messages from the icon. To capture and handle these messages we have to declare a new method in our form. For example:

  type
    TForm1 = class(TForm)
      ...
    private
      { Private declarations }
      ...
      procedure Icontray(var Msg: TMessage); message WM_ICONTRAY;
    public
      { Public declarations }
      ...
    end;

In the implementation of this method we can for example display a popup menu when the user right-clicks on the icon. Assuming you have created a popup menu named PopupMenu1, the following code will do the trick:

  procedure TForm1.Icontray(var Msg: TMessage);
  var
    CursorPos : TPoint;
  begin
    if Msg.lParam = WM_RBUTTONDOWN then begin
      GetCursorPos(CursorPos);
      SetForegroundWindow(Handle);        // suggested by Berend Radstaat
      PopupMenu1.Popup(CursorPos.x, CursorPos.y);
      PostMessage(Handle, WM_NULL, 0, 0); // suggested by Berend Radstaat
    end else
      inherited;
  end;

Thanks to Berend Radstaat for the additions to this article.


Showing and hiding the form
---------------------------

Normally you will include a menu item in the popup menu to show the form, so all you have to do is write an event handler to call the Show method of the form and optionally remove the icon from the system tray:

  procedure TForm1.mnuShowClick(Sender: TObject);
  begin
    Show;
    // Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);
  end;

When the user closes the form, you can for example hide it instead of closing it and optionally show the icon again in the system tray if you have removed it when you made the form visible. For example:

  procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  begin
    Action := caNone;
    Hide;
    // Shell_NotifyIcon(NIM_ADD, @NotifyIconData);
  end;


Finishing the application
-------------------------

You can have a menu item in the popup menu to exit the application. In this case you have to write an event handler for its click event to remove the icon and finish the application:

  procedure TForm1.mnuExitClick(Sender: TObject);
  begin
    Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);
    Application.ProcessMessages;
    Application.Terminate;
  end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
texas hold
     texas hold (Mar 31 2006 10:49PM)

autograph,optima rupturing capturers?Adams:working rashly spirits online poker [url=http://www.poker-stake.com/online-poker.html] online poker [/url] online poker http://www.poker-stake.com/online-poker.html http://www.poker-stake.com/online-poker.html artless incentive whom poker table [url=http://www.poker-stake.com/poker-table.html] poker table [/url] poker table http://www.poker-stake.com/poker-table.html http://www.poker-stake.com/poker-table.html Mayo denominator caviar.vulgarly free online poker [url=http://www.poker-stake.com/free-online-poker.html] free online poker [/url] free online poker http://www.poker-stake.com/free-online-poker.html http://www.poker-stake.com/free-online-poker.html plight Oklahoman,Catskills!ferried, internet poker [url=http://www.poker-stake.com/internet-poker.html] internet poker [/url] internet poker http://www.poker-stake.com/internet-poker.html http://www.poker-stake.com/internet-poker.html divans.blankness biasing skips hold em [url=http://www.poker-stake.com/hold-em.html] hold em [/url] hold em http://www.poker-stake.com/hold-em.html http://www.poker-stake.com/hold-em.html ennui originate poker bonus [url=http://www.poker-stake.com/poker-bonus.html] poker bonus [/url] poker bonus http://www.poker-stake.com/poker-bonus.html
Also, I noticed through the Windows task manager under Windows 2000 that you can significantly reduce the memory usage of your application while it is in the system tray by ensuring the application object is minimized when the form is closed, so I modified the close procedure to:

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caNone;
  Hide;
  application.Minimize;
  // Shell_NotifyIcon(NIM_ADD, @NotifyIconData);
end;

Finally, since the program starts in the tray without an active window, I added the application.minimize line to the project source just before the Application.run line to reduce memory usage.
Respond

RE: RE: RE: Strange Behavior On Minimize
Danny Weidig (Mar 28 2001 6:42PM)

Actually, the application.minimize should be listed before the hide for the close event to work properly. In the last comment, a button will be left on the taskbar. The button will also appear in the taskbar on the application start if the application.minimize is used in the project source, so maybe there is a different way if memory use is critical.
Respond

RE: RE: RE: RE: Strange Behavior On Minimize
Simon Suter (Apr 2 2001 1:33PM)

The solution is to use "Application.ShowMainForm := False;" instead of "Application.Minimize" before "Application.Run" in the project source.
Respond

System tray
    Coolest (Jan 18 2001 5:43PM)

for some reason I can not get the popup menu too work in your example. any help would be great full. thank you for you time
Respond

little corretion
    carlo muccini (Dec 9 2000 5:04AM)

TO SHOW THE ICON you must correct  the FormCreate Procedure
adding Application. before Icone.Hadle;

e.g.:
procedure TForm1.FormCreate(Sender: TObject);
begin
    with NotifyIconData do begin
      hIcon := Application.Icon.Handle;
      .....
Respond

RE: little corretion
Altin Karaulli (Jan 26 2001 2:52AM)

The user must right click on the tray icon and press Show on the popup menu to show the application. How can the application be shown directly on doubleclick on the tray icon ?
Respond

Double-Click in the icon
David T. Souchard (Jan 26 2001 11:16AM)

You can do it with a little change in the Icontray method:

  procedure TForm1.Icontray(var Msg: TMessage);
  var
    CursorPos : TPoint;
  begin
    if Msg.lParam = WM_LBUTTONDBLCLK then
      Show;
    else
      inherited;
  end;

Respond

Double-Click in the icon
David T. Souchard (Jan 26 2001 11:16AM)

You can do it with a little change in the Icontray method:

  procedure TForm1.Icontray(var Msg: TMessage);
  begin
    if Msg.lParam = WM_LBUTTONDBLCLK then
      Show
    else
      inherited;
  end;

Respond

Double-Click instead of right click
Ernesto De Spirito (Jan 26 2001 11:20AM)

You can do it with a little change in the Icontray method:

  procedure TForm1.Icontray(var Msg: TMessage);
  begin
    if Msg.lParam = WM_LBUTTONDBLCLK then
      Show
    else
      inherited;
  end;


Respond

Double-click instead of right-click
Ernesto De Spirito (Jan 27 2001 9:12AM)

You can do it with a little change in the Icontray method:

  procedure TForm1.Icontray(var Msg: TMessage);
  begin
    if Msg.lParam = WM_LBUTTONDBLCLK then
      Show
    else
      inherited;
  end;

Respond

RE: RE: little corretion
Ernesto De Spirito (Jan 27 2001 10:10AM)

You can do it with a little change in the Icontray method:

  procedure TForm1.Icontray(var Msg: TMessage);
  begin
    if Msg.lParam = WM_LBUTTONDBLCLK then
      Show
    else
      inherited;
  end;

Respond

RE: RE: RE: little corretion
Simon Suter (Apr 2 2001 1:45PM)

Try this little correction to do both pop menu on right button and double click to show application window:

procedure TForm1.Icontray(var Msg: TMessage);
var
   CursorPos : TPoint;
begin

   case Msg.lParam of
   WM_RBUTTONDOWN:
      begin
         GetCursorPos(CursorPos);
         PopupMenu1.Popup(CursorPos.x, CursorPos.y);
      end;
   WM_LBUTTONDBLCLK:
      begin
         Show;
         Application.Restore;
      end;
   else
      inherited;
   end;
end;
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)