Главная страница | назад





Article #17911: Creating a Win95 Shell icon application.

 Question and Answer Database
FAQ2911D.txt Creating a Win95 Shell icon application.
Category :Windows API
Platform :All
Product :All 32 bit
Question:
How do I create a Delphi/C++ Builder application that runs in the
system tray?
Answer:
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,
TrayIt1 in 'TrayIt1.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.ShowMainForm := False;
Application.CreateForm(TForm1, Form1);
ShowWindow(Application.Handle, SW_HIDE);
Application.Run;
end.
{TrayIt1.pas}
unit TrayIt1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ShellAPI, ExtCtrls;
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
Open1: TMenuItem;
Exit1: TMenuItem;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Open1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
procedure WndProc(var Msg : TMessage); override;
public
{ Public declarations }
IconData : TNotifyIconData;
IconCount : integer;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.WndProc(var Msg : TMessage);
var
p : TPoint;
begin
case Msg.Msg of
WM_USER + 1:
case Msg.lParam of
WM_RBUTTONDOWN: begin
GetCursorPos(p);
PopupMenu1.Popup(p.x, p.y);
end
end;
end;
inherited;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
BorderIcons := [biSystemMenu];
IconCount := 0;
IconData.cbSize := sizeof(IconData);
IconData.Wnd := Handle;
IconData.uID := 100;
IconData.uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
IconData.uCallbackMessage := WM_USER + 1;
IconData.hIcon := Application.Icon.Handle;
StrPCopy(IconData.szTip, Application.Title);
Shell_NotifyIcon(NIM_ADD, @IconData);
Timer1.Interval := 1000;
Timer1.Enabled := true;
end;
procedure TForm1.Open1Click(Sender: TObject);
begin
Form1.Show;
ShowWindow(Application.Handle, SW_HIDE);
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @IconData);
Application.ProcessMessages;
Application.Terminate;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Form1.Hide;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
case(IconCount) of
0 : IconData.hIcon := LoadIcon(0, IDI_APPLICATION);
1 : 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/16/98 4:31:28 PM

Last Modified: 01-SEP-99