Solved

minimize to tray

Posted on 2009-04-12
2
1,203 Views
Last Modified: 2012-06-27
I am trying to make my application run only in the system tray.  I have it set to hide when minimized but it only works once.  The hide seems to have no effect the second time.

I am using the trayicon component and applicationevents component of Delphi 2009 running on WindowsVista
procedure Ttcau.ApplicationEvents1Minimize(Sender: TObject);
begin
  hide;
end;
 
procedure Ttcau.ApplicationEvents1Restore(Sender: TObject);
begin
  show;
  bringtofront;
end;
 
procedure Ttcau.TrayIcon1DblClick(Sender: TObject);
begin
 if visible then
   begin
     application.Minimize;
     hide;
   end
     else
    begin
      application.Restore;
      show;
      application.bringtofront;
    end;
 end;

Open in new window

0
Comment
Question by:DSOM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 24128084
you'll need to remember the state of the application too in a separate variable
there is different places the form can be hidden or closed ...

this adds a menu option to the TrayIcon1 to close the application

Hitting the close button of the form, minimizes it to the tray
type
  TForm1 = class(TForm)
    TrayIcon1: TTrayIcon;
    PopupMenu1: TPopupMenu;
    popCloseApplication1: TMenuItem;
    procedure TrayIcon1DblClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure popCloseApplication1Click(Sender: TObject);
  private
    fAppVisible: Boolean;    
    fAppClosing: Boolean;
  public
    constructor Create(AOwner: TComponent); override;
  end;
 
constructor TForm1.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fAppVisible := True;
  fAppClosing := True;
end;
 
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := fAppClosing;
  if not CanClose then
  begin
    Hide;
    fAppVisible := False;
  end;
end;
 
procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
  if not fAppVisible then
  begin
    Self.Show;
    Self.BringToFront;
  end else
  begin
    Self.Hide;
  end;
  fAppVisible := not fAppVisible;
end;
 
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := fAppClosing;
  if not CanClose then
  begin
    Hide;
    fAppVisible := False;
  end;
end;
 
procedure TForm1.popCloseApplication1Click(Sender: TObject);
begin
  fAppClosing := True;
  Close;
end;

Open in new window

0
 

Author Closing Comment

by:DSOM
ID: 31569393
It worked except I needed to change:

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fAppVisible := True;
  fAppClosing := False;
end;

Thank you very much!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LAN or WAN ? 11 105
creating threads in delphi 1 161
Convert MS Word document to a PDF file 9 114
Delphi...Split view - idea? 1 114
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question