My first Icon to Tray program

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值