Solved

minimize to tray

Posted on 2009-04-12
2
1,194 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now