Solved

Delphi 6 and Tray Icon

Posted on 2001-08-06
14
535 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:Richard Coffre
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 21

Expert Comment

by:gemarti
ID: 6359058
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++++++++++++++++++++
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6359493
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.

????
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 6360143
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.
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6360347
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
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6360541
You can have a look to LeechFTP to see an example of what I described below.

I will retry your example tomorrow.

;-))

Richard
0
 
LVL 22

Expert Comment

by:mnasman
ID: 6361336
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
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6361493
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?
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 6

Expert Comment

by:MannSoft
ID: 6361909
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.
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6362817
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
0
 
LVL 22

Accepted Solution

by:
mnasman earned 50 total points
ID: 6364971
Hello

  Now I think here's the code you want, I added a button to the titlebar, that's will minimize the application to the tray, I hope that what you asking for

unit Unit1;

interface

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


const
  WM_MYTRAYICONCALLBACK = WM_USER + 1000;
  htTrayBtn = htSizeLast + 1;

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);
    procedure FormResize(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    TrayBtn : TRect;
    MyTrayIcon: TNotifyIconData;
    procedure AppMinimize;
    procedure WMMyTrayIconCallback(var Msg:TMessage);
                       message WM_MYTRAYICONCALLBACK;
    procedure DrawCaptButton;
    procedure WMNCPaint(var Msg : TWMNCPaint); message WM_NCPaint;
    procedure WMNCActivate(var Msg : TWMNCActivate); message WM_NCACTIVATE;
    procedure WMSetText(var Msg : TWMSetText); message WM_SETTEXT;
    procedure WMNCHitTest(var Msg : TWMNCHitTest); message WM_NCHITTEST;
    procedure WMNCLButtonDown(var Msg : TWMNCLButtonDown); message WM_NCLBUTTONDOWN;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DrawCaptButton;
var
  xFrame,
  yFrame,
  xSize,
  ySize  : Integer;
  R : TRect;
begin
  xFrame := GetSystemMetrics(SM_CXFRAME);
  yFrame := GetSystemMetrics(SM_CYFRAME);

  xSize  := GetSystemMetrics(SM_CXSIZE);
  ySize  := GetSystemMetrics(SM_CYSIZE);

  TrayBtn := Bounds(Width - xFrame - 4*xSize + 2,
                       yFrame + 2, xSize - 2, ySize - 4);

  Canvas.Handle := GetWindowDC(Self.Handle);

  Canvas.Font.Name := 'Wingdings';
  Canvas.Font.Color := clBlue;
  Canvas.Font.Size := 8;
  Canvas.Font.Style := [fsBold];
  Canvas.Pen.Color := clYellow;
  Canvas.Brush.Color := clBtnFace;

  try
    DrawButtonFace(Canvas, TrayBtn, 1, bsAutoDetect, False, False, False);
    R := Bounds(Width - xFrame - 4 * xSize + 2,
                       yFrame + 3, xSize - 6, ySize - 7);
    with TrayBtn do
      Canvas.TextRect(R, R.Left + 2, R.Top - 1, '?');
  finally
    ReleaseDC(Self.Handle, Canvas.Handle);
    Canvas.Handle := 0;
  end;
end;

procedure TForm1.WMNCPaint(var Msg : TWMNCPaint);
begin
  inherited;
  DrawCaptButton;
end;

procedure TForm1.WMNCActivate(var Msg : TWMNCActivate);
begin
  inherited;
  DrawCaptButton;
end;

procedure TForm1.WMSetText(var Msg : TWMSetText);
begin
  inherited;
  DrawCaptButton;
end;

procedure TForm1.WMNCHitTest(var Msg : TWMNCHitTest);
begin
  inherited;
  with Msg do
    if PtInRect(TrayBtn, Point(XPos - Left, YPos - Top)) then
      Result := htTrayBtn;
end;

procedure TForm1.WMNCLButtonDown(var Msg : TWMNCLButtonDown);
begin
  inherited;
  if (Msg.HitTest = htTrayBtn) then
    AppMinimize;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  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';
end;

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

procedure TForm1.AppMinimize;
begin
  Shell_NotifyIcon(NIM_ADD, @MyTrayIcon);
  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
  Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);
  Application.Restore;
  ShowWindow(Application.Handle, SW_Show);
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  Perform(WM_NCACTIVATE, Word(Active), 0);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Shell_NotifyIcon(NIM_DELETE, @MyTrayIcon);
end;

end.

Best regards
Mohammed Nasman
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6366996
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 ?
0
 
LVL 22

Expert Comment

by:mnasman
ID: 6367006
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
0
 
LVL 22

Expert Comment

by:mnasman
ID: 6367088
give me ur email, or send me email at nasman@mogaza.org, and i will send u the project
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6367164
Thanks a lot
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

10 Experts available now in Live!

Get 1:1 Help Now