Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 631
  • Last Modified:

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
0
Richard Coffre
Asked:
Richard Coffre
  • 5
  • 4
  • 3
  • +1
1 Solution
 
gemartiCommented:
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
 
Richard CoffreAuthor Commented:
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
 
MannSoftCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gemartiCommented:
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
 
Richard CoffreAuthor Commented:
You can have a look to LeechFTP to see an example of what I described below.

I will retry your example tomorrow.

;-))

Richard
0
 
Mohammed NasmanSoftware DeveloperCommented:
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
 
gemartiCommented:
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
 
MannSoftCommented:
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
 
Richard CoffreAuthor Commented:
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
 
Mohammed NasmanSoftware DeveloperCommented:
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
 
Richard CoffreAuthor Commented:
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
 
Mohammed NasmanSoftware DeveloperCommented:
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
 
Mohammed NasmanSoftware DeveloperCommented:
give me ur email, or send me email at nasman@mogaza.org, and i will send u the project
0
 
Richard CoffreAuthor Commented:
Thanks a lot
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now