uses
messages, AppEvnts, Messages, Controls,...
...
const
WM_MYICONNOTIFY = WM_USER +
123
;
type
TForm1 =
class
(TForm)
...
PopupMenu1: TPopupMenu;
...
public
procedure
WMICON(
var
msg: TMessage); message WM_MYICONNOTIFY;
procedure
WMSYSCOMMAND(
var
msg: TMessage);message WM_SYSCOMMAND;
procedure
RestoreMainForm;
procedure
HideMainForm;
procedure
CreateTrayIcon(n:
Integer
);
procedure
DeleteTrayIcon(n:
Integer
);
...
uses
ComObj, ShellApi,...
procedure
TForm1
.
WMICON(
var
msg: TMessage);
var
P : TPoint;
nidata : TNotifyIconData;
begin
case
msg
.
LParam
of
WM_LBUTTONDOWN, WM_RBUTTONDOWN:
begin
GetCursorPos(p);
SetForegroundWindow(Application
.
MainForm
.
Handle);
PopupMenu1
.
Popup(P
.
X, P
.
Y);
end
;
WM_LBUTTONDBLCLK :
begin
RestoreMainForm;
DeleteTrayIcon(
1
);
SetForeGroundWindow(Application
.
MainForm
.
Handle);
SetActiveWindow(Application
.
MainForm
.
Handle);
end
;
WM_MOUSEMOVE:
begin
with
nidata
do
begin
cbSize := SizeOf(TNotifyIconData);
Wnd := Self
.
Handle;
uID :=
1
;
uFlags := NIF_ICON
or
NIF_MESSAGE
or
NIF_TIP;
uCallBackMessage := WM_MYICONNOTIFY;
hIcon := Application
.
Icon
.
Handle;
StrPCopy(szTip,Application
.
Title+
'текст всплывающей подсказки'
);
end
;
Shell_NotifyIcon(NIM_MODIFY, @nidata);
end
;
end
;
end
;
procedure
TForm1
.
WMSYSCOMMAND(
var
msg: TMessage);
begin
inherited
;
if
(Msg
.
wParam=SC_MINIMIZE)
then
begin
HideMainForm;
CreateTrayIcon(
1
);
end
;
end
;
procedure
TForm1
.
HideMainForm;
begin
Application
.
ShowMainForm :=
False
;
ShowWindow(Application
.
Handle, SW_HIDE);
ShowWindow(Application
.
MainForm
.
Handle, SW_HIDE);
end
;
procedure
TForm1
.
RestoreMainForm;
var
i,j :
Integer
;
begin
Application
.
ShowMainForm :=
True
;
ShowWindow(Application
.
Handle, SW_RESTORE);
ShowWindow(Application
.
MainForm
.
Handle, SW_RESTORE);
if
not
ShownOnce
then
begin
for
I :=
0
to
Application
.
MainForm
.
ComponentCount -
1
do
if
Application
.
MainForm
.
Components[i]
is
TWinControl
then
with
Application
.
MainForm
.
Components[i]
as
TWinControl
do
if
Visible
then
begin
ShowWindow(Handle, SW_SHOWDEFAULT);
for
J :=
0
to
ComponentCount -
1
do
if
Components[J]
is
TWinControl
then
ShowWindow((Components[J]
as
TWinControl).Handle, SW_SHOWDEFAULT);
end
;
ShownOnce :=
True
;
end
;
end
;
procedure
TForm1
.
CreateTrayIcon(n:
Integer
);
var
nidata : TNotifyIconData;
begin
with
nidata
do
begin
cbSize := SizeOf(TNotifyIconData);
Wnd := Self
.
Handle;
uID :=
1
;
uFlags := NIF_ICON
or
NIF_MESSAGE
or
NIF_TIP;
uCallBackMessage := WM_MYICONNOTIFY;
hIcon := Application
.
Icon
.
Handle;
StrPCopy(szTip,Application
.
Title+
'текст всплывающей подсказки'
);
end
;
Shell_NotifyIcon(NIM_ADD, @nidata);
end
;
procedure
TForm1
.
DeleteTrayIcon(n:
Integer
);
var
nidata : TNotifyIconData;
begin
with
nidata
do
begin
cbSize := SizeOf(TNotifyIconData);
Wnd := Self
.
Handle;
uID :=
1
;
end
;
Shell_NotifyIcon(NIM_DELETE, @nidata);
end
;
procedure
TForm1
.
FormCreate(Sender: TObject);
begin
ShownOnce:=
False
;
CreateTrayIcon(
1
);
end
;
procedure
TForm1
.
FormDestroy(Sender: TObject);
begin
DeleteTrayIcon(
1
);
end
;