peration: TOperation);
begin
if ( Operation = opRemove ) then
if AComponent = FRightMenu then
FRightMenu := Nil
else
if AComponent = FLeftMenu then
FLeftMenu := Nil;
end;
procedure TFnugryTrayIcon.Change;
begin
if assigned(FOnChange) then
FOnChange(Self);
end;
procedure TFnugryTrayIcon.AllocateTrayIcon;
begin
assert(assigned(FIcon));
if FHandle = 0 then
begin
FHandle := AllocateHWnd(TrayWndProc);
CheckError(FHandle <> 0, msg_err_allocwnd);
try
fillchar(FIconData, Sizeof(FIconData), 0);
FIconData.cbSize := sizeof(FIconData);
FIconData.uFlags := NIF_ICON OR NIF_MESSAGE OR NIF_TIP;
FIconData.uCallbackMessage := WM_ICONNOTIFY;
FIconData.Wnd := FHandle;
FIconData.hIcon := FIcon.Handle;
StrCopy(FIconData.szTip, PChar(FTip));
CheckError(Shell_NotifyIcon(NIM_ADD, @FIconData), msg_err_trayadd);
except
DeallocateHWnd(FHandle);
FHandle := 0;
raise;
end;
end;
end;
procedure TFnugryTrayIcon.ReleaseTrayIcon;
begin
if FHandle <> 0 then
begin
CheckError(Shell_NotifyIcon(NIM_DELETE, @FIconData), msg_err_traydelete);
DeallocateHWnd(FHandle);
FHandle := 0;
end;
end;
procedure TFnugryTrayIcon.UpdateTrayIcon;
begin
assert(FHandle <> 0);
FIconData.cbSize := sizeof(FIconData);
FIconData.Wnd := FHandle;
FIconData.uFlags := NIF_ICON OR NIF_TIP;
FIconData.uCallbackMessage := WM_ICONNOTIFY;
FIconData.Wnd := FHandle;
FIconData.hIcon := FIcon.Handle;
StrCopy(FIconData.szTip, PChar(FTip));
CheckError(Shell_NotifyIcon(NIM_MODIFY, @FIconData), msg_err_traymodify);
end;
constructor TFnugryTrayIcon.Create(AOwner :TComponent);
begin
inherited Create(AOwner);
FIcon := TIcon.Create;
FIcon.ReleaseHandle;
FIcon.Handle := LoadIcon(0, IDI_APPLICATION);
FIcon.OnChange := evIconChange;
end;
destructor TFnugryTrayIcon.Destroy;
begin
if Visible then ReleaseTrayIcon;
FTip := '';
FIcon.Free;
inherited Destroy;
end;
procedure TFnugryTrayIcon.CheckError(fCond :Boolean; MsgID :Integer);
begin
if not fCond then
raise ETrayIconError.CreateRes(MsgID);
end;
procedure TFnugryTrayIcon.TrayWndProc(var Msg :TMessage);
begin
msg.result := 0;
case Msg.lParam of
WM_MOUSEMOVE : MouseMove;
WM_LBUTTONDBLCLK : MouseLBtnDblClk;
WM_RBUTTONDBLCLK : MouseRBtnDblClk;
WM_LBUTTONUP : MouseLBtnUp;
WM_RBUTTONUP : MouseRBtnUp;
WM_LBUTTONDOWN : MouseLBtnDown;
WM_RBUTTONDOWN : MouseRBtnDown;
else
msg.result := DefWindowProc(FHandle, Msg.Msg, msg.wParam, msg.lParam);
end;
end;
procedure TFnugryTrayIcon.MouseMove;
begin
if assigned(FOnMouseMove) then
FOnMouseMove(Self);
end;
procedure TFnugryTrayIcon.MouseLBtnUp;
var
ptCursor :TPoint;
begin
if assigned(FLeftMenu) then
begin
GetCursorPos(ptCursor);
FLeftMenu.Popup(ptCursor.x, ptCursor.y);
end
else
if assigned(FOnLBtnUp) then
FOnLBtnUp(Self);
end;
procedure TFnugryTrayIcon.MouseRBtnUp;
var
ptCursor :TPoint;
begin
if assigned(FRightMenu) then
begin
GetCursorPos(ptCursor);
FRightMenu.Popup(ptCursor.x, ptCursor.y);
end
else
if assigned(FOnRBtnUp) then
FOnRBtnUp(Self);
end;
procedure TFnugryTrayIcon.MouseLBtnDown;
begin
if assigned(FOnLBtnDown) then
FOnLBtnDown(Self);
end;
procedure TFnugryTrayIcon.MouseRBtnDown;
begin
if assigned(FOnRBtnDown) then
FOnRBtnDown(Self);
end;
procedure TFnugryTrayIcon.MouseLBtnDblClk;
begin
if assigned(FOnLBtnDblClk) then
FOnLBtnDblClk(Self);
end;
procedure TFnugryTrayIc