unit Unit_TrayIcon;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellAPI, StdCtrls, Menus;
//add shellAPI unit
const
WM_TrayIconMsg=WM_USER+100;
// define user message
type
TFrm_TrayIcon = class(TForm)
Label1: TLabel;
PopupMenu1: TPopupMenu;
aa1: TMenuItem; //add menu item
bb1: TMenuItem;
cc1: TMenuItem;
ExitE1: TMenuItem;//menuItem Exit Application
N1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure ExitE1Click(Sender: TObject);
private
{ Private declarations }
myTrayIcon:TNotifyIconData;
//declare a class of TNotifyIconData structure
procedure AddTrayIcon();//Add icon to Tray
procedure delTrayIcon(); //delete icon from Tray
procedure TrayIconMsg(var msg:TMessage);
message WM_TrayIconMsg; //message procedure of the user define
procedure sysCmdMsg(var msg:TWMSysCommand);
message WM_SYSCOMMAND;//system message---'WM_SYSCOMMAND'
public
{ Public declarations }
end;
var
Frm_TrayIcon: TFrm_TrayIcon;
implementation
{$R *.dfm}
procedure TFrm_TrayIcon.AddTrayIcon();
begin
Fillchar(myTrayIcon,sizeOf(TNotifyIconData),0);
// zeroMemory(@myTrayIcon,sizeOf(TNotifyIconData)) ;
myTrayIcon.cbSize:=sizeOf(TNotifyIconData);
myTrayIcon.Wnd:=Handle;
myTrayIcon.uID:=12; // define Icon id
myTrayIcon.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
//msg scope
myTrayIcon.uCallbackMessage:=WM_TrayIconMsg;
//callbackmessage
myTrayIcon.hIcon:=Application.Icon.Handle; //Icon hwnl
myTrayIcon.szTip:='Twei Tray Icon Example'; //Tips
shell_NotifyIcon(NIM_ADD,@myTrayIcon); //add Icon
end;
procedure TFrm_TrayIcon.delTrayIcon();
begin
myTrayIcon.cbSize:=sizeOf(TNotifyIconData);
myTrayIcon.Wnd:=handle;
myTrayIcon.uID:=12; //Icon id
shell_NotifyIcon(NIM_DELETE,@myTrayIcon); //delete Icon
end;
procedure TFrm_TrayIcon.TrayIconMsg(var msg:TMessage);
var
mousePos:TPoint; //mouse point
begin
//left button of mouse double click event
if (msg.LParam=WM_LBUTTONDBLCLK) then // show() ;
showwindow(Frm_TrayIcon.Handle,sw_normal);
if (msg.LParam=WM_RBUTTONDOWN) then //right button of mouse
begin
setActiveWindow(handle); //Active window
getCursorPos(mousePos);
PopupMenu1.Popup(MousePos.X,mousePos.Y);
end;
end;
procedure TFrm_TrayIcon.FormCreate(Sender: TObject);
begin
Frm_TrayIcon.FormStyle:=fsStayOnTop;
//On task Toolbar hide the Application
setWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
AddTrayIcon();
end;
procedure TFrm_TrayIcon.ExitE1Click(Sender: TObject);
begin
if Application.MessageBox('Are you sure?','Exit',mb_YESNO)=IDYES then
begin
delTrayIcon();
Application.Terminate;
end;
end;
procedure TFrm_TrayIcon.sysCmdMsg(var msg:TWMSysCommand);
begin
if (msg.CmdType=SC_CLOSE) or (msg.CmdType=SC_MINIMIZE) then
begin
showWindow(Frm_TrayIcon.Handle,sw_Hide);
setWindowLong(Application.Handle,GWL_EXSTYLE, S_EX_TOOLWINDOW);
Hide;
end //
else inherited;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellAPI, StdCtrls, Menus;
//add shellAPI unit
const
WM_TrayIconMsg=WM_USER+100;
// define user message
type
TFrm_TrayIcon = class(TForm)
Label1: TLabel;
PopupMenu1: TPopupMenu;
aa1: TMenuItem; //add menu item
bb1: TMenuItem;
cc1: TMenuItem;
ExitE1: TMenuItem;//menuItem Exit Application
N1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure ExitE1Click(Sender: TObject);
private
{ Private declarations }
myTrayIcon:TNotifyIconData;
//declare a class of TNotifyIconData structure
procedure AddTrayIcon();//Add icon to Tray
procedure delTrayIcon(); //delete icon from Tray
procedure TrayIconMsg(var msg:TMessage);
message WM_TrayIconMsg; //message procedure of the user define
procedure sysCmdMsg(var msg:TWMSysCommand);
message WM_SYSCOMMAND;//system message---'WM_SYSCOMMAND'
public
{ Public declarations }
end;
var
Frm_TrayIcon: TFrm_TrayIcon;
implementation
{$R *.dfm}
procedure TFrm_TrayIcon.AddTrayIcon();
begin
Fillchar(myTrayIcon,sizeOf(TNotifyIconData),0);
// zeroMemory(@myTrayIcon,sizeOf(TNotifyIconData)) ;
myTrayIcon.cbSize:=sizeOf(TNotifyIconData);
myTrayIcon.Wnd:=Handle;
myTrayIcon.uID:=12; // define Icon id
myTrayIcon.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
//msg scope
myTrayIcon.uCallbackMessage:=WM_TrayIconMsg;
//callbackmessage
myTrayIcon.hIcon:=Application.Icon.Handle; //Icon hwnl
myTrayIcon.szTip:='Twei Tray Icon Example'; //Tips
shell_NotifyIcon(NIM_ADD,@myTrayIcon); //add Icon
end;
procedure TFrm_TrayIcon.delTrayIcon();
begin
myTrayIcon.cbSize:=sizeOf(TNotifyIconData);
myTrayIcon.Wnd:=handle;
myTrayIcon.uID:=12; //Icon id
shell_NotifyIcon(NIM_DELETE,@myTrayIcon); //delete Icon
end;
procedure TFrm_TrayIcon.TrayIconMsg(var msg:TMessage);
var
mousePos:TPoint; //mouse point
begin
//left button of mouse double click event
if (msg.LParam=WM_LBUTTONDBLCLK) then // show() ;
showwindow(Frm_TrayIcon.Handle,sw_normal);
if (msg.LParam=WM_RBUTTONDOWN) then //right button of mouse
begin
setActiveWindow(handle); //Active window
getCursorPos(mousePos);
PopupMenu1.Popup(MousePos.X,mousePos.Y);
end;
end;
procedure TFrm_TrayIcon.FormCreate(Sender: TObject);
begin
Frm_TrayIcon.FormStyle:=fsStayOnTop;
//On task Toolbar hide the Application
setWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
AddTrayIcon();
end;
procedure TFrm_TrayIcon.ExitE1Click(Sender: TObject);
begin
if Application.MessageBox('Are you sure?','Exit',mb_YESNO)=IDYES then
begin
delTrayIcon();
Application.Terminate;
end;
end;
procedure TFrm_TrayIcon.sysCmdMsg(var msg:TWMSysCommand);
begin
if (msg.CmdType=SC_CLOSE) or (msg.CmdType=SC_MINIMIZE) then
begin
showWindow(Frm_TrayIcon.Handle,sw_Hide);
setWindowLong(Application.Handle,GWL_EXSTYLE, S_EX_TOOLWINDOW);
Hide;
end //
else inherited;
end;
end.