Jeg formoder således du er mest til komplette svar ... her er et sådan...
Du tager en nyt delphi projekt. Ændere din dpr fil til det følgende:
program TrayIcon;
uses
Windows,
Forms,
UnitTrayIcon in 'UnitTrayIcon.pas' {Form1},
Dialogs,
SysUtils;
{$R *.RES}
const
MemFileSize = 1024;
MemFileName = 'Tray Icon Test';
begin
CreateFileMapping(HWND($FFFFFFFF), nil,PAGE_READWRITE,0,MemFileSize, MemFileName);
if GetLastError <> ERROR_ALREADY_EXISTS
then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.ShowMainForm := UpperCase(ParamStr(1)) <> '-START';
Application.Run;
end
else
MessageDlg('One Instance Only', mtError, [mbOK],0);
end.
Så tager du og ændere unit1 til det følgende :
unit UnitTrayIcon;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, Menus, ShellAPi;
const
wm_IconMessage = wm_User;
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
Details1: TMenuItem;
Close1: TMenuItem;
N1: TMenuItem;
About1: TMenuItem;
procedure Details1Click(Sender: TObject);
procedure Close1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
public
nid: TNotifyIconData;
Procedure InitializeNID;
procedure IconTray (var Msg: TMessage); message wm_IconMessage;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
Procedure TForm1.InitializeNID;
begin
ShowWindow(Application.Handle, SW_HIDE);
Icon.Handle := LoadIcon (HInstance, 'MAINICON');
nid.cbSize := sizeof (nid);
nid.wnd := Handle;
nid.uID := 1; // icon ID
nid.uCallBAckMessage := wm_IconMessage;
nid.hIcon := Icon.Handle;
nid.uFlags := nif_Message or nif_Icon or nif_Tip;
strcopy (nid.szTip, PChar('Et lille HINT'));
Shell_NotifyIcon (NIM_ADD, @nid);
end;
procedure TForm1.IconTray (var msg: TMessage);
var
Pt: TPoint;
begin
if Msg.lParam = wm_rbuttondown then
begin
GetCursorPos (Pt);
PopupMenu1.Popup (Pt.x, Pt.y);
end;
if Msg.lParam = wm_lbuttondblclk then
Details1Click (self);
end;
procedure TForm1.Details1Click(Sender: TObject);
begin
nid.uFlags := 0;
Shell_NotifyIcon (NIM_DELETE, @nid);
ShowWindow (Handle, sw_ShowNormal);
SetForegroundWindow (Handle);
Show;
end;
procedure TForm1.Close1Click(Sender: TObject);
begin
Application.Terminate;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
InitializeNID;
Action := caNone;
ShowWindow (Handle, SW_HIDE);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
nid.uFlags := 0;
Shell_NotifyIcon (NIM_DELETE, @nid);
end;
procedure TForm1.About1Click(Sender: TObject);
begin
MessageDlg ('Notify Icon Data Demo' + #13+ ' - By Jens Borrisholt' + #13 + ' © 1998', mtInformation, [mbOk], 0);
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if key = 27 then
Close;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
InitializeNID;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
InitializeNID;
ShowWindow (Application.Handle, SW_HIDE);
//If you omit this line the application will be shown in the taskbar when
//the application is visible
end;
end.
Eksemplet er det TrayIcon eksempel du finder på
http://borrisholt.com. Blot har jeg tilføjet to linjer i dpr filen.
Jens B