home *** CD-ROM | disk | FTP | other *** search
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
-
- #include "TrayComp.h"
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- // ValidCtrCheck is used to assure that the components created do not have
- // any pure virtual functions.
- //
-
- static inline void ValidCtrCheck(TTrayComp *)
- {
- new TTrayComp(NULL);
- }
- //---------------------------------------------------------------------------
- __fastcall TTrayComp::TTrayComp(TComponent* Owner)
- : TComponent(Owner)
- {
- FIcon = new TIcon();
- FTimer = new TTimer(NULL);
-
- FIconIndex = 0;
- FIcon->Assign(Application->Icon);
- FAppRestore = arLeftDblClick;
- FPopupMenuShow = arNone;
- FVisible = false;
- FHide = true;
- FTimer->Enabled = false;
- FTimer->OnTimer = OnAnimation;
- FTimer->Interval = 1000;
-
- if (!ComponentState.Contains(csDesigning))
- {
- memset(&FNid, 0, sizeof(TNotifyIconData));
- FNid.cbSize = sizeof(TNotifyIconData);
- FNid.hWnd = AllocateHWnd(OnMessage);
- FNid.uID = (UINT)this;
- FNid.hIcon = FIcon->Handle;
- FNid.uFlags = NIF_ICON | NIF_MESSAGE;
- FNid.uCallbackMessage = WM_SYSTEM_TRAY_NOTIFY;
-
- // Replace the application's OnMinimize and OnRestore handlers with
- // special ones for the tray icon. The TrayComp component has its own
- // OnMinimize and OnRestore events that the user can set.
- Application->OnMinimize = TrayCompMinimize;
- Application->OnRestore = TrayCompRestore;
- Update();
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TTrayComp::~TTrayComp()
- {
- if (!ComponentState.Contains(csDesigning))
- Shell_NotifyIcon(NIM_DELETE, &FNid);
-
- if (FIcon)
- delete FIcon;
-
- if (FTimer)
- delete FTimer;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::SetVisible(bool Value)
- {
- FVisible = Value;
-
- if (!ComponentState.Contains(csDesigning))
- {
- if (FVisible)
- {
- if (!Shell_NotifyIcon(NIM_ADD, &FNid))
- {
- ::MessageBox(Application->Handle, "Cannot Create System Shell Notification Icon",
- "Application Error", MB_OK);
-
- Application->Terminate();
- }
- Hide = true;
- Application->OnMinimize = TrayCompMinimize;
- Application->OnRestore = TrayCompRestore;
- }
- else
- {
- if (!Shell_NotifyIcon(NIM_DELETE, &FNid))
- {
- ::MessageBox(Application->Handle, "Cannot Remove System Shell Notification Icon",
- "Application Error", MB_OK);
-
- Application->Terminate();
- }
- Hide = false;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::TrayCompMinimize(TObject* Sender)
- {
- Minimize();
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::TrayCompRestore(TObject* Sender)
- {
- Restore();
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::SetHint(String Hint)
- {
- // The new hint must be different than previous hint and less than
- // 64 characters to be modified.
- if ((FHint != Hint) && (Hint.Length() < 64))
- {
- FHint = Hint;
- StrPLCopy(FNid.szTip, Hint, sizeof(FNid.szTip) - 1);
-
- // If there is no hint then there is no tool tip.
- if (Hint.Length())
- FNid.uFlags = FNid.uFlags | NIF_TIP;
- else
- FNid.uFlags = FNid.uFlags & !NIF_TIP;
-
- Update();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::SetHide(bool Value)
- {
- FHide = Value;
- }
- //---------------------------------------------------------------------------
- int __fastcall TTrayComp::GetInterval()
- {
- return FTimer->Interval;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::SetInterval(int Value)
- {
- FTimer->Interval = Value;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTrayComp::GetAnimate()
- {
- return FTimer->Enabled;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::SetAnimate(bool Value)
- {
- if (FIconList)
- FTimer->Enabled = Value;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::OnMessage(TMessage &Message)
- {
- switch(Message.Msg)
- {
- case WM_QUERYENDSESSION:
- Message.Result = 1;
- break;
-
- case WM_ENDSESSION:
- EndSession();
- break;
-
- case WM_SYSTEM_TRAY_NOTIFY:
- switch(Message.LParam)
- {
- case WM_LBUTTONDOWN:
- LButtonDown();
- break;
-
- case WM_LBUTTONUP:
- LButtonUp();
- break;
-
- case WM_LBUTTONDBLCLK:
- LButtonDblClick();
- break;
-
- case WM_RBUTTONDOWN:
- RButtonDown();
- break;
-
- case WM_RBUTTONUP:
- RButtonUp();
- break;
-
- case WM_RBUTTONDBLCLK:
- RButtonDblClick();
- break;
- }
- }
-
- TComponent::Dispatch(&Message);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::LButtonDown()
- {
- if (FAppRestore == arLeftClick)
- Restore();
- if (FPopupMenuShow == arLeftClick)
- ShowMenu();
-
- if (FOnLButtonDown)
- FOnLButtonDown(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::LButtonUp()
- {
- if (FAppRestore == arLeftClickUp)
- Restore();
- if (FPopupMenuShow == arLeftClickUp)
- ShowMenu();
-
- if (FOnLButtonUp)
- FOnLButtonUp(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::LButtonDblClick()
- {
- if (FAppRestore == arLeftDblClick)
- Restore();
- if (FPopupMenuShow == arLeftDblClick)
- ShowMenu();
-
- if (FOnLButtonDblClick)
- FOnLButtonDblClick(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::RButtonDown()
- {
- if (FAppRestore == arRightClick)
- Restore();
- if (FPopupMenuShow == arRightClick)
- ShowMenu();
-
- if (FOnRButtonDown)
- FOnRButtonDown(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::RButtonUp()
- {
- if (FAppRestore == arRightClickUp)
- Restore();
- if (FPopupMenuShow == arRightClickUp)
- ShowMenu();
-
- if (FOnRButtonUp)
- FOnRButtonUp(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::RButtonDblClick()
- {
- if (FAppRestore == arRightDblClick)
- Restore();
- if (FPopupMenuShow == arRightDblClick)
- ShowMenu();
-
- if (FOnRButtonDblClick)
- FOnRButtonDblClick(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::EndSession()
- {
- Shell_NotifyIcon(NIM_DELETE, &FNid);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::Loaded()
- {
- TComponent::Loaded();
-
- if (!FIconList)
- FIcon->Assign(Application->Icon);
- else
- FIconList->GetIcon(FIconIndex, FIcon);
-
- Update();
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::Minimize()
- {
- Application->Minimize();
-
- if (FHide && FVisible)
- ShowWindow(Application->Handle, SW_HIDE);
-
- if (FOnMinimize)
- FOnMinimize(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::Restore()
- {
- Application->Restore();
-
- if (FHide && FVisible)
- {
- ShowWindow(Application->Handle, SW_RESTORE);
- SetForegroundWindow(Application->Handle);
- }
-
- if (FOnRestore)
- FOnRestore(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::Update()
- {
- if (!ComponentState.Contains(csDesigning))
- {
- FNid.hIcon = FIcon->Handle;
- Shell_NotifyIcon(NIM_MODIFY, &FNid);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::ShowMenu()
- {
- TPoint *Point = new TPoint;
-
- if (FPopupMenu)
- {
- GetCursorPos((tagPOINT*)Point);
- FPopupMenu->Popup(Point->x, Point->y);
- }
-
- delete Point;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::SetIconIndex(int Value)
- {
- if (!ComponentState.Contains(csDesigning))
- {
- FIconIndex = Value;
-
- if (FIconList)
- FIconList->GetIcon(FIconIndex, FIcon);
- Update();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::OnAnimation(TObject* Sender)
- {
- if (IconIndex < FIconList->Count)
- FIconIndex++;
- else
- FIconIndex = 0;
-
- SetIconIndex(FIconIndex);
- Update();
- }
- //---------------------------------------------------------------------------
- void __fastcall TTrayComp::SetDefaultIcon()
- {
- FIcon->Assign(Application->Icon);
- Update();
- }
- //---------------------------------------------------------------------------
- namespace Traycomp
- {
- void __fastcall PACKAGE Register()
- {
- TComponentClass classes[1] = {__classid(TTrayComp)};
- RegisterComponents("System", classes, 0);
- }
- }
- //---------------------------------------------------------------------------
-