Solved

Tray icon

Posted on 2002-05-16
7
1,782 Views
Last Modified: 2012-08-14
InThe posted this code [shown below] (it's in the archives).

It works just fine for me with one exception....

The first time I minimize the App with the minimize button (top right corner), it minimizes to the tray.

Then I restore the app to the desktop.  So far so good.

The second (and subsequent) attempts to minimize have no effect.  Clicking the Minimize button does nothing.

Using D6 on Win XP Home.


******code*************

hi,
you can either use the code like below or can get a trayicon component form www.torry.net


const
WM_MYTRAYICONCALLBACK = WM_USER + 1000;

type
TForm1 = class(TForm)
  PopupMenu1 : TPopupMenu;
  Close1     : TMenuItem;
  Restore1   : TMenuItem;
  Timer1     : TTimer;
  procedure Close1Click(Sender: TObject);
  procedure Restore1Click(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
  procedure Timer1Timer(Sender: TObject);
private
  { Private declarations }
  HIcon1, HIcon2 : HICON;
  MyTrayIcon: TNotifyIconData;
  procedure AppMinimize(Sender:TObject);
  procedure WMMyTrayIconCallback(var Msg:TMessage);
                     message WM_MYTRAYICONCALLBACK;
public
  { Public declarations }
end;

{...}

procedure TForm1.FormCreate(Sender: TObject);
begin
{ load our icons }
HIcon1 := LoadIcon(0, IDI_WINLOGO);
HIcon2 := LoadIcon(0, IDI_HAND);

{ set TApplictaion.OnMinimize to our new event }
Application.OnMinimize := AppMinimize;

{ start our application minimized to the System Tray }
Visible := FALSE;
Application.ShowMainForm := Visible;

{ required to set cbSize to SizeOf our structure }
MyTrayIcon.cbSize := SizeOf(TNotifyIconData);

{ handle to main window }
MyTrayIcon.Wnd    := Handle;

{ identifier of our icon }
MyTrayIcon.uId    := 1;

{ flags showing what we are using in the other members below }
MyTrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;

{ our user defined message }
MyTrayIcon.uCallBackMessage := WM_MYTRAYICONCALLBACK;

{  set our icon to the first one loaded above }
MyTrayIcon.hIcon  := HIcon1;

{ help message of the icon }
MyTrayIcon.szTip  := 'Lou''s Delphi Tip of the Day [MyTrayIcon]';

{ add our icon to the tray }
Shell_NotifyIcon(NIM_ADD, @MyTrayIcon);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
{ remove our icon from the tray }
Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);
end;

procedure TForm1.AppMinimize(Sender:TObject);
begin
{ hide our application }
Visible := FALSE;
Application.ShowMainForm := Visible;
end;

procedure TForm1.WMMyTrayIconCallback(var Msg:TMessage);
var
CursorPos : TPoint;
begin
case Msg.lParam of
  WM_LBUTTONDOWN :
    begin
      { get mouse cursor position }
      GetCursorPos(CursorPos);
      { popup our TPopupMenu }
      PopupMenu1.Popup(CursorPos.X,CursorPos.Y);
    end;
end;
end;

procedure TForm1.Close1Click(Sender: TObject);
begin
{ close our application }
Close;
end;

procedure TForm1.Restore1Click(Sender: TObject);
begin
{ restore our application, or minimize if already
  in a restore mode }
Visible := TRUE;
Application.ShowMainForm := Visible;
SetForegroundWindow(Application.Handle);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
{ change our icon to the opposite one loaded }
if MyTrayIcon.hIcon = HIcon1 then
  MyTrayIcon.hIcon := HIcon2
else
  MyTrayIcon.hIcon := HIcon1;

{ tell the SystemTray to modify our changes }
Shell_NotifyIcon(NIM_MODIFY, @MyTrayIcon);
end;  

0
Comment
Question by:d32coder
7 Comments
 

Author Comment

by:d32coder
ID: 7013345
I placed a button on the form that points to the AppMinimize function and it works on every attempt.  Somehow the Application.OnMinimize is not responding to the MINIMIZE button after the first use.  weird...

Don
0
 
LVL 3

Expert Comment

by:raidos
ID: 7013394
my guess that it doesn't try to minimize the window since it is allready minimized...or something..

in your Restore1Click add:
  Form1.WindowState = wsNormal;

Not tested just a guess..

Regards
//raidos
0
 
LVL 3

Accepted Solution

by:
raidos earned 200 total points
ID: 7013406
When i need to do something like this I hook the WM_SYSCOMMAND Message and put a case statement for the SC_MINIMIZE command... to do exactly what you did in your AppMinimize...like this..

Type TForm1 = Class(TForm)
     ...
     Private
       Procedure WMSYSCOMMAND(Var Msg: TWMSyscommand); Message WM_SYSCOMMAND;
       ...
     End;

{-----}
Implementation

Procedure TForm1.WMSYSCOMMAND(Var Msg: TWMSyscommand);
Begin
  Case Msg.CmdType Of
    SC_MINIMIZE : Begin
      Visible := FALSE;
      Application.ShowMainForm := FALSE;
    End;
    Else Inherited;
End;
0
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.

 
LVL 17

Expert Comment

by:geobul
ID: 7013425
Hi,

Try this:

procedure TForm1.AppMinimize(Sender:TObject);
begin
  { hide our application }
  Visible := FALSE;
  Application.MainForm.Visible := Visible;
end;

Regards, Geo
0
 

Author Comment

by:d32coder
ID: 7013453
That fixed it.  thanks!

Don
0
 
LVL 17

Expert Comment

by:inthe
ID: 7013463
hi,
i was just testing to see what on earth was going on and what messages to catch (i had same problem on win2000 when testig ) so i just started afresh one using WM_SYSCOMMAND as raidos mentioned,i see the question is solved now but the following works ok so i'll post it here anyway, maybe someone can use it ..:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,shellapi,
  Menus;

  const
  WM_MYTRAYICONCALLBACK = WM_USER + 1000;

  type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    Restore1: TMenuItem;
    Close1: TMenuItem;
 procedure FormCreate(Sender: TObject);
    procedure Close1Click(Sender: TObject);
    procedure Restore1Click(Sender: TObject);
  private
   HIcon1 : HICON;
   procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;
   procedure WMMyTrayIconCallback(var Message:TMessage);message WM_MYTRAYICONCALLBACK;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  TrayIcon : PNotifyIconData;
implementation

{$R *.DFM}

procedure TForm1.WMSysCommand(var Message:TMessage);

 begin
  if Message.WParam = SC_MINIMIZE then
  begin
    TrayIcon := new(PNotifyIconDataA);
    TrayIcon.cbSize := SizeOf(PNotifyIconDataA);
    TrayIcon.Wnd := Form1.Handle;
    TrayIcon.hIcon := HIcon1;
    TrayIcon.uCallbackMessage := WM_MYTRAYICONCALLBACK;
    TrayIcon.uID :=0;
    TrayIcon.szTip := 'Test';
    TrayIcon.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
    Shell_NotifyIcon(NIM_ADD,TrayIcon);
    Dispose(TrayIcon);
    Form1.Visible := False;
  end
   else
  begin
   DefWindowProc(Form1.Handle,Message.Msg,Message.WParam,Message.LParam);
  end;
end;

procedure TForm1.WMMyTrayIconCallback(var Message:TMessage);
var
p : tpoint;
begin
  if (Message.LParam = WM_LBUTTONDOWN) then
   begin
     GetCursorPos(p);
     PopupMenu1.Popup(p.X,p.Y);
//if you dont want popupmenu then uncomment the below lines
//    Shell_NotifyIcon(NIM_DELETE,TrayIcon);
//    Form1.Visible := True;
   end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
HIcon1 := LoadIcon(0, IDI_APPLICATION);
end;


procedure TForm1.Close1Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.Restore1Click(Sender: TObject);
begin
//if dont want popupmenu delete these lines
    Shell_NotifyIcon(NIM_DELETE,TrayIcon);
    Form1.Visible := True;
end;

end.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 7015782
thanx Barry.
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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

14 Experts available now in Live!

Get 1:1 Help Now