00 Votes

Delphi/Lazarus: Show Application with Icon in System Tray

Question by Guest | 2015-03-19 at 14:15

In Windows, there is this Taskbar Notification Area or TNA in the down right of the screen (sometimes also referred as System Tray, Systray or Tray) in which you can find the icons of some applications.

How can I implement this using Delphi or Lazarus? I would like to show the main window of my program when clicking the SysTray Icon and I would also like to have the possibility to let the window disappear to this location (for example when minimizing the window or with clicking a button).

Does someone have a tip for me how to do it?

ReplyPositiveNegative
Avatar
3Best Answer3 Votes

In the tab "Additional", you can find the component TTrayIcon. When dropping this to you application, you should have all you need.

You can exactly adjust the behavior in the properties of TrayIcon to your needs.

  • The property "Visible" determines whether your TrayIcon is visible or not. You can directly set the visibility to "true", so that the Icon is immediately visible after starting the program or you can set visible to "false", first, in order to change it during runtime.
  • Under "Icon", you can select an image that is used as icon in the taskbar.
  • Even the property "PopUpMenu" is interesting, because here you can define a popup-menu that will be shown whenever the user right clicks the icon with his mouse.

Assumed, you have a button on your form, you can realize the following, for example:

procedure TForm1.Button1Click(Sender: TObject);
begin
   Form1.Hide;
   TrayIcon1.Visible:=true;
end;

With this, your window is hidden and simultaneously shown as TrayIcon.

Of course, you can also do the same when the user is minimizing the window:

procedure TForm1.FormWindowStateChange(Sender: TObject);
begin
  if Form1.WindowState = wsminimized then begin
     Form1.Hide;
     TrayIcon1.Visible:=true;   
  end;
end;      

With this, the window is directly minimized to the tray.

To get back the window, you can write the following code into the OnDblClick event of the TrayIcon:

procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
  Form1.WindowState:=wsNormal; 
  Form1.Show;    
  Form1.SetFocus;   
  TrayIcon1.Visible:=false;
end;

In other words, when double-clicking the TrayIcon, the window will be displayed again, the window gets the focus and the TraycIcon is set to invisible.

I think with those snippets, you have enough sources to implement what you need and of course, you can adjust all of those functions to your requirements.
2015-03-22 at 03:19

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.