destructor TDCPTrayIcon.Destroy;
begin
if FCurrentlyActive then
RemoveTrayIcon;
FTrayIcon.Free;
if FNotificationHandle <> 0 then
DeallocateHWnd(FNotificationHandle);
inherited Destroy;
end;
procedure TDCPTrayIcon.DoDblClick;
begin
if Assigned(OnDblClick) then
OnDblClick(self);
end;
procedure TDCPTrayIcon.Loaded;
begin
inherited Loaded;
if not (csDesigning in ComponentState) then begin
if FTrayIcon.Handle = 0 then
FTrayIcon.Assign(Application.Icon);
end;
SetTrayIcon(FTrayIcon);
if FActive then
ShowTrayIcon;
end;
procedure TDCPTrayIcon.NotificationWndProc(var Message: TMessage);
var
Pt: TPoint;
begin
if Message.Msg = UWM_TRAYICON then begin
case Message.lParam of
WM_LBUTTONDBLCLK:
DoDblClick;
WM_RBUTTONDOWN:
if Assigned(FPopupMenu) then begin
SetForegroundWindow(FNotificationHandle);
GetCursorPos(Pt);
FPopupMenu.Popup(Pt.X, Pt.Y);
end;
end;
end;
end;
procedure TDCPTrayIcon.OnAppMinimize(Sender: TObject);
begin
if FCurrentlyActive then
RemoveFromTaskbar;
end;
procedure TDCPTrayIcon.RemoveFromTaskbar;
begin
if not (csDesigning in ComponentState) then begin
ShowWindow(Application.Handle, SW_HIDE);
ShowWindow(Application.MainForm.Handle, SW_HIDE);
end;
end;
procedure TDCPTrayIcon.RemoveTrayIcon;
begin
if FCurrentlyActive then
if SendTrayMessage(NIM_DELETE) then begin
FCurrentlyActive := False;
ShowInTaskbar;
end;
end;
procedure TDCPTrayIcon.RestoreApp;
begin
ShowInTaskbar;
SetForegroundWindow(Application.MainForm.Handle);
end;
function TDCPTrayIcon.SendTrayMessage(AMessage: DWORD): Boolean;
var
Flags: Integer;
NotifyIconData: TNotifyIconData;
begin
Result := True;
if csDesigning in ComponentState then
exit;
Flags := NIF_MESSAGE or NIF_ICON;
if FShowHint then
Flags := Flags or NIF_TIP;
FillChar(NotifyIconData, SizeOf(NotifyIconData), 0);
with NotifyIconData do begin
cbSize := SizeOf(TNotifyIconData);
Wnd := FNotificationHandle;
uID := ID_TRAYICON;
uFlags := Flags;
uCallBackMessage := UWM_TRAYICON;
hIcon := FTrayIcon.Handle;
StrLCopy(szTip, PChar(FHint), SizeOf(szTip));
end;
Result := Shell_NotifyIcon(AMessage, @NotifyIconData);
end;
procedure TDCPTrayIcon.SetActive(const Value: Boolean);
begin
FActive := Value;
if FActive then
ShowTrayIcon
else
RemoveTrayIcon;
end;
procedure TDCPTrayIcon.SetHint(const Value: string);
begin
FHint := Value;
SendTrayMessage(NIM_MODIFY);
end;
procedure TDCPTrayIcon.SetPopupMenu(const Value: TPopupMenu);
begin
FPopupMenu := Value;
end;
procedure TDCPTrayIcon.SetShowHint(const Value: Boolean);
begin
FShowHint := Value;
SendTrayMessage(NIM_MODIFY);
end;
procedure TDCPTrayIcon.SetTrayIcon(const Value: TIcon);
begin
FTrayIcon.Assign(Value);
SendTrayMessage(NIM_MODIFY);
end;
procedure TDCPTrayIcon.ShowInTaskbar;
begin
if not (csDesigning in ComponentState) then begin
if Application.MainForm <> nil then
ShowWindow(Application.MainForm.Handle, SW_SHOWNORMAL);
ShowWindow(Application.Handle, SW_SHOWNORMAL);
end;
end;
procedure TDCPTrayIcon.ShowTrayIcon;
begin
if SendTrayMessage(NIM_ADD) then
FCurrentlyActive := True;
end;