home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / ikona.exe / traymain.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  5KB  |  182 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #include <shellapi.h>
  8. #pragma hdrstop
  9.  
  10. #include "traymain.h"
  11. //---------------------------------------------------------------------------
  12. #pragma resource "*.dfm"
  13. TForm1 *Form1;
  14. //---------------------------------------------------------------------------
  15. __fastcall TForm1::TForm1(TComponent* Owner)
  16.   : TForm(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TForm1::DrawItem(TMessage& Msg)
  21. {
  22.      IconDrawItem((LPDRAWITEMSTRUCT)Msg.LParam);
  23.      TForm::Dispatch(&Msg);
  24. }
  25. //---------------------------------------------------------------------------
  26. void __fastcall TForm1::MyNotify(TMessage& Msg)
  27. {
  28.     POINT MousePos;
  29.  
  30.     switch(Msg.LParam)
  31.     {
  32.         case WM_RBUTTONUP:
  33.             if (GetCursorPos(&MousePos))
  34.             {
  35.                 PopupMenu1->PopupComponent = Form1;
  36.                 SetForegroundWindow(Handle);
  37.                 PopupMenu1->Popup(MousePos.x, MousePos.y);
  38.             }
  39.             else
  40.                 Show();
  41.             break;
  42.         case WM_LBUTTONUP:
  43.             ToggleState();
  44.             break;
  45.         default:
  46.             break;
  47.     }
  48.     TForm::Dispatch(&Msg);
  49. }
  50. //---------------------------------------------------------------------------
  51. void __fastcall TForm1::FormDestroy(TObject *Sender)
  52. {
  53.     TrayMessage(NIM_DELETE);
  54. }
  55. //---------------------------------------------------------------------------
  56. bool __fastcall TForm1::TrayMessage(DWORD dwMessage)
  57. {
  58.    NOTIFYICONDATA tnd;
  59.    PSTR pszTip;
  60.  
  61.    pszTip = TipText();
  62.  
  63.    tnd.cbSize          = sizeof(NOTIFYICONDATA);
  64.    tnd.hWnd            = Handle;
  65.    tnd.uID             = IDC_MYICON;
  66.    tnd.uFlags          = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  67.    tnd.uCallbackMessage    = MYWM_NOTIFY;
  68.  
  69.    if (dwMessage == NIM_MODIFY)
  70.     {
  71.         tnd.hIcon        = IconHandle();
  72.         if (pszTip)
  73.            lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
  74.         else
  75.         tnd.szTip[0] = '\0';
  76.     }
  77.    else
  78.     {
  79.         tnd.hIcon = NULL;
  80.         tnd.szTip[0] = '\0';
  81.     }
  82.  
  83.    return (Shell_NotifyIcon(dwMessage, &tnd));
  84. }
  85. //---------------------------------------------------------------------------
  86. HANDLE __fastcall TForm1::IconHandle(void)
  87. {
  88.     if (RadioButton1->Checked)
  89.         return (Image1->Picture->Icon->Handle);
  90.     else
  91.         return (Image2->Picture->Icon->Handle);
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TForm1::ToggleState(void)
  95. {
  96.     if (RadioButton1->Checked)
  97.     {
  98.         RadioButton1->Checked = false;
  99.         RadioButton2->Checked = true;
  100.     }
  101.     else
  102.     {
  103.         RadioButton2->Checked = false;
  104.         RadioButton1->Checked = true;
  105.     }
  106.     TrayMessage(NIM_MODIFY);
  107. }
  108. //---------------------------------------------------------------------------
  109. PSTR __fastcall TForm1::TipText(void)
  110. {
  111.     if (RadioButton1->Checked)
  112.         return (Edit1->Text.c_str());
  113.     else
  114.         return (Edit2->Text.c_str());
  115. }
  116. //---------------------------------------------------------------------------
  117. void __fastcall TForm1::CheckBox1Click(TObject *Sender)
  118. {
  119.     if (CheckBox1->Checked)
  120.     {
  121.         TrayMessage(NIM_ADD);
  122.         TrayMessage(NIM_MODIFY);
  123.     }
  124.     else
  125.         TrayMessage(NIM_DELETE);
  126.  
  127.     Button1->Enabled = CheckBox1->Checked;
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TForm1::Button1Click(TObject *Sender)
  131. {
  132.     Hide();
  133. }
  134. //---------------------------------------------------------------------------
  135. void __fastcall TForm1::RadioButtonClick(TObject *Sender)
  136. {
  137.     if (!CheckBox1->Checked)
  138.         return;
  139.  
  140.     TrayMessage(NIM_MODIFY);
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TForm1::EditKeyUp(TObject *Sender, WORD &Key,
  144.     TShiftState Shift)
  145. {
  146.     if (!CheckBox1->Checked)
  147.         return;
  148.  
  149.     TrayMessage(NIM_MODIFY);
  150. }
  151. //---------------------------------------------------------------------------
  152. LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi)
  153. {
  154.     HICON hIcon;
  155.  
  156.     hIcon = (HICON)LoadImage(g_hinst, MAKEINTRESOURCE(lpdi->CtlID), IMAGE_ICON,
  157.         16, 16, 0);
  158.     if (!hIcon)
  159.         return(FALSE);
  160.  
  161.     DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon,
  162.         16, 16, 0, NULL, DI_NORMAL);
  163.  
  164.     return(TRUE);
  165. }
  166. //---------------------------------------------------------------------------
  167. void __fastcall TForm1::Properties1Click(TObject *Sender)
  168. {
  169.     Show();    
  170. }
  171. //---------------------------------------------------------------------------
  172. void __fastcall TForm1::ToggleState1Click(TObject *Sender)
  173. {
  174.     ToggleState();
  175. }
  176. //---------------------------------------------------------------------------
  177. void __fastcall TForm1::Shutdown1Click(TObject *Sender)
  178. {
  179.     Close();    
  180. }
  181. //---------------------------------------------------------------------------
  182.