Solved

Tray icon

Posted on 2002-05-16
7
1,779 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

11 Experts available now in Live!

Get 1:1 Help Now