home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Examples / Controls / Tray / TRAYCOMP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-26  |  10.0 KB  |  364 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "TrayComp.h"
  6. #pragma package(smart_init)
  7. //---------------------------------------------------------------------------
  8. // ValidCtrCheck is used to assure that the components created do not have
  9. // any pure virtual functions.
  10. //
  11.  
  12. static inline void ValidCtrCheck(TTrayComp *)
  13. {
  14.    new TTrayComp(NULL);
  15. }
  16. //---------------------------------------------------------------------------
  17. __fastcall TTrayComp::TTrayComp(TComponent* Owner)
  18.    : TComponent(Owner)
  19. {
  20.    FIcon = new TIcon();
  21.    FTimer = new TTimer(NULL);
  22.  
  23.    FIconIndex = 0;
  24.    FIcon->Assign(Application->Icon);
  25.    FAppRestore = arLeftDblClick;
  26.    FPopupMenuShow = arNone;
  27.    FVisible = false;
  28.    FHide = true;
  29.    FTimer->Enabled = false;
  30.    FTimer->OnTimer = OnAnimation;
  31.    FTimer->Interval = 1000;
  32.  
  33.    if (!ComponentState.Contains(csDesigning))
  34.    {
  35.       memset(&FNid, 0, sizeof(TNotifyIconData));
  36.       FNid.cbSize = sizeof(TNotifyIconData);
  37.       FNid.hWnd = AllocateHWnd(OnMessage);
  38.       FNid.uID = (UINT)this;
  39.       FNid.hIcon = FIcon->Handle;
  40.       FNid.uFlags = NIF_ICON | NIF_MESSAGE;
  41.       FNid.uCallbackMessage = WM_SYSTEM_TRAY_NOTIFY;
  42.  
  43.       // Replace the application's OnMinimize and OnRestore handlers with
  44.       // special ones for the tray icon. The TrayComp component has its own
  45.       // OnMinimize and OnRestore events that the user can set.
  46.       Application->OnMinimize = TrayCompMinimize;
  47.       Application->OnRestore = TrayCompRestore;
  48.       Update();
  49.    }
  50. }
  51. //---------------------------------------------------------------------------
  52. __fastcall TTrayComp::~TTrayComp()
  53. {
  54.    if (!ComponentState.Contains(csDesigning))
  55.       Shell_NotifyIcon(NIM_DELETE, &FNid);
  56.  
  57.    if (FIcon)
  58.       delete FIcon;
  59.  
  60.    if (FTimer)
  61.       delete FTimer;
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TTrayComp::SetVisible(bool Value)
  65. {
  66.    FVisible = Value;
  67.  
  68.    if (!ComponentState.Contains(csDesigning))
  69.    {
  70.       if (FVisible)
  71.       {
  72.          if (!Shell_NotifyIcon(NIM_ADD, &FNid))
  73.          {
  74.             ::MessageBox(Application->Handle, "Cannot Create System Shell Notification Icon",
  75.                "Application Error", MB_OK);
  76.  
  77.             Application->Terminate();
  78.          }
  79.          Hide = true;
  80.          Application->OnMinimize = TrayCompMinimize;
  81.          Application->OnRestore = TrayCompRestore;         
  82.       }
  83.       else
  84.       {
  85.          if (!Shell_NotifyIcon(NIM_DELETE, &FNid))
  86.          {
  87.             ::MessageBox(Application->Handle, "Cannot Remove System Shell Notification Icon",
  88.                "Application Error", MB_OK);
  89.  
  90.             Application->Terminate();
  91.          }
  92.          Hide = false;
  93.       }
  94.    }
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TTrayComp::TrayCompMinimize(TObject* Sender)
  98. {
  99.    Minimize();
  100. }
  101. //---------------------------------------------------------------------------
  102. void __fastcall TTrayComp::TrayCompRestore(TObject* Sender)
  103. {
  104.    Restore();
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TTrayComp::SetHint(String Hint)
  108. {
  109.    // The new hint must be different than previous hint and less than
  110.    // 64 characters to be modified.
  111.    if ((FHint != Hint) && (Hint.Length() < 64))
  112.    {
  113.       FHint = Hint;
  114.       StrPLCopy(FNid.szTip, Hint, sizeof(FNid.szTip) - 1);
  115.  
  116.       // If there is no hint then there is no tool tip.
  117.       if (Hint.Length())
  118.          FNid.uFlags = FNid.uFlags | NIF_TIP;
  119.       else
  120.          FNid.uFlags = FNid.uFlags & !NIF_TIP;
  121.  
  122.       Update();
  123.    }
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TTrayComp::SetHide(bool Value)
  127. {
  128.    FHide = Value;
  129. }
  130. //---------------------------------------------------------------------------
  131. int __fastcall TTrayComp::GetInterval()
  132. {
  133.    return FTimer->Interval;
  134. }
  135. //---------------------------------------------------------------------------
  136. void __fastcall TTrayComp::SetInterval(int Value)
  137. {
  138.    FTimer->Interval = Value;
  139. }
  140. //---------------------------------------------------------------------------
  141. bool __fastcall TTrayComp::GetAnimate()
  142. {
  143.    return FTimer->Enabled;
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TTrayComp::SetAnimate(bool Value)
  147. {
  148.    if (FIconList)
  149.       FTimer->Enabled = Value;
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TTrayComp::OnMessage(TMessage &Message)
  153. {
  154.    switch(Message.Msg)
  155.    {
  156.       case WM_QUERYENDSESSION:
  157.          Message.Result = 1;
  158.          break;
  159.  
  160.       case WM_ENDSESSION:
  161.          EndSession();
  162.          break;
  163.  
  164.       case WM_SYSTEM_TRAY_NOTIFY:
  165.          switch(Message.LParam)
  166.          {
  167.             case WM_LBUTTONDOWN:
  168.                LButtonDown();
  169.                break;
  170.  
  171.             case WM_LBUTTONUP:
  172.                LButtonUp();
  173.                break;
  174.  
  175.             case WM_LBUTTONDBLCLK:
  176.                LButtonDblClick();
  177.                break;
  178.  
  179.             case WM_RBUTTONDOWN:
  180.                RButtonDown();
  181.                break;
  182.  
  183.             case WM_RBUTTONUP:
  184.                RButtonUp();
  185.                break;
  186.  
  187.             case WM_RBUTTONDBLCLK:
  188.                RButtonDblClick();
  189.                break;
  190.          }
  191.    }
  192.  
  193.    TComponent::Dispatch(&Message);
  194. }
  195. //---------------------------------------------------------------------------
  196. void __fastcall TTrayComp::LButtonDown()
  197. {
  198.    if (FAppRestore == arLeftClick)
  199.       Restore();
  200.    if (FPopupMenuShow == arLeftClick)
  201.       ShowMenu();
  202.  
  203.         if (FOnLButtonDown)
  204.         FOnLButtonDown(this);
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall TTrayComp::LButtonUp()
  208. {
  209.    if (FAppRestore == arLeftClickUp)
  210.       Restore();
  211.    if (FPopupMenuShow == arLeftClickUp)
  212.       ShowMenu();
  213.  
  214.         if (FOnLButtonUp)
  215.         FOnLButtonUp(this);
  216. }
  217. //---------------------------------------------------------------------------
  218. void __fastcall TTrayComp::LButtonDblClick()
  219. {
  220.    if (FAppRestore == arLeftDblClick)
  221.       Restore();
  222.    if (FPopupMenuShow == arLeftDblClick)
  223.       ShowMenu();
  224.  
  225.         if (FOnLButtonDblClick)
  226.         FOnLButtonDblClick(this);
  227. }
  228. //---------------------------------------------------------------------------
  229. void __fastcall TTrayComp::RButtonDown()
  230. {
  231.    if (FAppRestore == arRightClick)
  232.       Restore();
  233.    if (FPopupMenuShow == arRightClick)
  234.       ShowMenu();
  235.  
  236.         if (FOnRButtonDown)
  237.         FOnRButtonDown(this);
  238. }
  239. //---------------------------------------------------------------------------
  240. void __fastcall TTrayComp::RButtonUp()
  241. {
  242.    if (FAppRestore == arRightClickUp)
  243.       Restore();
  244.    if (FPopupMenuShow == arRightClickUp)
  245.       ShowMenu();
  246.  
  247.         if (FOnRButtonUp)
  248.         FOnRButtonUp(this);
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TTrayComp::RButtonDblClick()
  252. {
  253.    if (FAppRestore == arRightDblClick)
  254.       Restore();
  255.    if (FPopupMenuShow == arRightDblClick)
  256.       ShowMenu();
  257.  
  258.         if (FOnRButtonDblClick)
  259.         FOnRButtonDblClick(this);
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TTrayComp::EndSession()
  263. {
  264.         Shell_NotifyIcon(NIM_DELETE, &FNid);
  265. }
  266. //---------------------------------------------------------------------------
  267. void __fastcall TTrayComp::Loaded()
  268. {
  269.    TComponent::Loaded();
  270.  
  271.    if (!FIconList)
  272.       FIcon->Assign(Application->Icon);
  273.    else
  274.       FIconList->GetIcon(FIconIndex, FIcon);
  275.  
  276.    Update();
  277. }
  278. //---------------------------------------------------------------------------
  279. void __fastcall TTrayComp::Minimize()
  280. {
  281.    Application->Minimize();
  282.  
  283.    if (FHide && FVisible)
  284.       ShowWindow(Application->Handle, SW_HIDE);
  285.  
  286.    if (FOnMinimize)
  287.       FOnMinimize(this);
  288. }
  289. //---------------------------------------------------------------------------
  290. void __fastcall TTrayComp::Restore()
  291. {
  292.    Application->Restore();
  293.  
  294.    if (FHide && FVisible)
  295.    {
  296.            ShowWindow(Application->Handle, SW_RESTORE);
  297.            SetForegroundWindow(Application->Handle);
  298.    }
  299.  
  300.         if (FOnRestore)
  301.                 FOnRestore(this);
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TTrayComp::Update()
  305. {
  306.         if (!ComponentState.Contains(csDesigning))
  307.    {
  308.       FNid.hIcon = FIcon->Handle;
  309.         Shell_NotifyIcon(NIM_MODIFY, &FNid);
  310.    }
  311. }
  312. //---------------------------------------------------------------------------
  313. void __fastcall TTrayComp::ShowMenu()
  314. {
  315.    TPoint *Point = new TPoint;
  316.  
  317.    if (FPopupMenu)
  318.    {
  319.       GetCursorPos((tagPOINT*)Point);
  320.       FPopupMenu->Popup(Point->x, Point->y);
  321.    }
  322.  
  323.    delete Point;
  324. }
  325. //---------------------------------------------------------------------------
  326. void __fastcall TTrayComp::SetIconIndex(int Value)
  327. {
  328.    if (!ComponentState.Contains(csDesigning))
  329.    {
  330.       FIconIndex = Value;
  331.  
  332.       if (FIconList)
  333.          FIconList->GetIcon(FIconIndex, FIcon);
  334.       Update();
  335.    }
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall TTrayComp::OnAnimation(TObject* Sender)
  339. {
  340.    if (IconIndex < FIconList->Count)
  341.       FIconIndex++;
  342.    else
  343.       FIconIndex = 0;
  344.  
  345.    SetIconIndex(FIconIndex);
  346.    Update();
  347. }
  348. //---------------------------------------------------------------------------
  349. void __fastcall TTrayComp::SetDefaultIcon()
  350. {
  351.    FIcon->Assign(Application->Icon);
  352.    Update();
  353. }
  354. //---------------------------------------------------------------------------
  355. namespace Traycomp
  356. {
  357.    void __fastcall PACKAGE Register()
  358.    {
  359.        TComponentClass classes[1] = {__classid(TTrayComp)};
  360.        RegisterComponents("System", classes, 0);
  361.    }
  362. }
  363. //---------------------------------------------------------------------------
  364.