Link to home
Start Free TrialLog in
Avatar of Richard Coffre
Richard CoffreFlag for France

asked on

Delphi 6 and Tray Icon

I know that in previous versions, you can use something called Tray Icon, how can I use this feature in Delphi 6 ?
An example ?

Thanks in advance
Avatar of gemarti
gemarti
Flag of United States of America image

Start a new project

Select an Icon for the form

Then use this code:

++++++++++++++cut below the line+++++++++++++++++


unit cshowicon;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI;

type
  TForm1 = class(TForm)
    procedure FormDestroy(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    pnid : TNotifyIconData;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
implementation


{$R *.dfm}

procedure TForm1.FormDestroy(Sender: TObject);
begin
 pnid.uFlags := 0;
 Shell_NotifyIcon(NIM_DELETE,@pnid);
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
 pnid.cbSize := SizeOf(pnid);
 pnid.Wnd := Form1.Handle;
 pnid.uID := 0;
 pnid.uCallbackMessage := WM_MOUSEMOVE;
 pnid.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
 pnid.hIcon := Form1.Icon.Handle;
 pnid.szTip := 'Tips';
 Shell_NotifyIcon(NIM_ADD,@pnid);
end;

end.

+++++++++++++++cut above the line++++++++++++++++++++
Avatar of Richard Coffre

ASKER

I tested it but when I run it, I don't see the specific button to iconify it as a tray icon. Normally it's a button at the left of the 3 usual : Minimize, Restore, Close.

????
Avatar of MannSoft
MannSoft

That would have been an extra feature of the tray component you were using.  Its not something that is standard which every tray application has, although it's nice.
I guess I misunderstood you. (seems to be the game played here these days...) I thought you wanted a tray Icon for your application. The tray Icon is the area where the digital clock is located on the Taskbar. If you've assigned an Icon to your form you'll see that Icon next to the clock on the taskbar when the application starts.

This button next to the MinMax/Position/Close buttons on the form (Iconify) sounds more like a Unix feature. I can't ever recall seeing this type of button on a windows application, but I've been wrong before.

There is most likely a way of doing this. I admitedly don't know how. Maybe one of the other experts will join in and give you your answer. I gave you what you orginally asked for basedon the detail of your question.

 ;]

GEM
You can have a look to LeechFTP to see an example of what I described below.

I will retry your example tomorrow.

;-))

Richard
Avatar of Mohammed Nasman
Hello

  Here's a sample for tray icon, It's work fine with Delphi 6(I test it with it), I hope that what are you looking for :)

//==
unit Unit1;

interface

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


const
  WM_MYTRAYICONCALLBACK = WM_USER + 1000;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMinimize := AppMinimize;
  ShowWindow(Application.Handle, SW_HIDE);
  MyTrayIcon.cbSize := SizeOf(TNotifyIconData);
  MyTrayIcon.Wnd    := Handle;
  MyTrayIcon.uId    := 1;
  MyTrayIcon.uFlags := NIF_ICON or NIF_TIP or NIF_MESSAGE;
  MyTrayIcon.uCallBackMessage := WM_MYTRAYICONCALLBACK;
  MyTrayIcon.hIcon  := Application.Icon.Handle;
  MyTrayIcon.szTip  := 'Click on Icon to see the options';
  Shell_NotifyIcon(NIM_ADD, @MyTrayIcon);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);
  Application.Terminate;
end;

procedure TForm1.AppMinimize(Sender:TObject);
begin
  Application.Minimize;
  ShowWindow(Application.Handle, SW_HIDE);
end;

procedure TForm1.WMMyTrayIconCallback(var Msg:TMessage);
var
  CursorPos : TPoint;
begin
  case Msg.lParam of
    WM_LBUTTONDOWN :
      begin
        GetCursorPos(CursorPos);
        PopupMenu1.Popup(CursorPos.X,CursorPos.Y);
      end;
  end;
end;

procedure TForm1.Close1Click(Sender: TObject);
begin
  Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);
  Application.Terminate;
end;

procedure TForm1.Restore1Click(Sender: TObject);
begin
  Application.Restore;
  ShowWindow(Application.Handle, SW_Show);
end;

end.

Best regards
Mohammed Nasman
hmm...this looks familiar Mohammed. Do you know how to put a 'Iconify' button on the forms' top bar next to the Min/maz - restore (or position) - close buttons?
Yea the real question is "How can I put a 4th button on the right of the caption/title bar?".  I know what he's talking about (most programs that have this 4th button use an image of what looks like a period, and performs "minimize to tray") which is where the confusion comes from.
I think that the last remark of MannSoft is really good and it is the right question.

So do you have an example because I'm a newbie with VCL ...

Thanks in advance,
Richard
ASKER CERTIFIED SOLUTION
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I copy directly the code above it raised some issues because I don't put the control on the form.
Then I found the control for the PopUp Menu but I don't find the one for "Close1     : TMenuItem;", I put a Toolbar but I'm unable to know what I have to do after.

Any ideas ?
before u copy the code to ur form
add popmenu, then add to items to it, first one is restore and the other is close
give me ur email, or send me email at nasman@mogaza.org, and i will send u the project
Thanks a lot