This is done via the ShellAPI function Shell_NotifyIcon(). The
following example shows how to create an application that uses this
function. The example application shows a different icon in the
system tray every second and shows the time in the title. The
application responds to a right click of the mouse by showing a menu
allowing the user to display a form or quit the application. Attempts
to close the form result in the form hiding, but the tray application
continues to run. Note that no task bar icon is shown for the form. To
compile the application, you will need to create a form with a
TPopupMenu and a TTimer component. You will also need to make
modifications to the main project source file (TrayIt.dpr).
Example:
{TrayIt.dpr}
program TrayIt;
uses
Windows,
Forms,
TrayIt in 'TrayIt .pas' {Form };
{$R *.RES}
begin
Application.Initialize;
Application.ShowMainForm := False;
Application.CreateForm(TForm , Form );
ShowWindow(Application.Handle, SW_HIDE);
Application.Run;
end.
{TrayIt .pas}
unit TrayIt ;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ShellAPI, ExtCtrls;
type
TForm = class(TForm)
PopupMenu : TPopupMenu;
Open : TMenuItem;
Exit : TMenuItem;
Timer : TTimer;
procedure FormCreate(Sender: TObject);
procedure Open Click(Sender: TObject);
procedure Exit Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer Timer(Sender: TObject);
private
{ Private declarations }
procedure WndProc(var Msg : TMessage); override;
public
{ Public declarations }
IconData : TNotifyIconData;
IconCount : integer;
end;
var
Form : TForm ;
implementation
{$R *.DFM}
procedure TForm .WndProc(var Msg : TMessage);
var
p : TPoint;
begin
case Msg.Msg of
WM_USER + :
case Msg.lParam of
WM_RBUTTONDOWN: begin
GetCursorPos(p);
PopupMenu .Popup(p.x, p.y);
end
end;
end;
inherited;
end;
procedure TForm .FormCreate(Sender: TObject);
begin
BorderIcons := [biSystemMenu];
IconCount := 0;
IconData.cbSize := sizeof(IconData);
IconData.Wnd := Handle;
IconData.uID := 00;
IconData.uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
IconData.uCallbackMessage := WM_USER + ;
IconData.hIcon := Application.Icon.Handle;
StrPCopy(IconData.szTip, Application.Title);
Shell_NotifyIcon(NIM_ADD, @IconData);
Timer .Interval := 000;
Timer .Enabled := true;
end;
procedure TForm .Open Click(Sender: TObject);
begin
Form .Show;
ShowWindow(Application.Handle, SW_HIDE);
end;
procedure TForm .Exit Click(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @IconData);
Application.ProcessMessages;
Application.Terminate;
end;
procedure TForm .FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Form .Hide;
end;
procedure TForm .Timer Timer(Sender: TObject);
begin
case(IconCount) of
0 : IconData.hIcon := LoadIcon(0, IDI_APPLICATION);
: IconData.hIcon := LoadIcon(0, IDI_ASTERISK);
2 : IconData.hIcon := LoadIcon(0, IDI_EXCLAMATION);
3 : IconData.hIcon := LoadIcon(0, IDI_HAND);
4 : IconData.hIcon := LoadIcon(0, IDI_QUESTION);
5 : IconData.hIcon := Application.Icon.Handle;
end;
inc(IconCount);
if IconCount > 5 then
IconCount := 0;
Application.Title := TimeToStr(Now);
StrPCopy(IconData.szTip, Application.Title);
Shell_NotifyIcon(NIM_MODIFY, @IconData);
end;
begin
ShowWindow(Application.Handle, SW_HIDE);
end.
7/ 6/98 4:3 :28 PM