home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / TRAYNOT.PAK / APP32.C next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.4 KB  |  319 lines

  1. //---------------------------------------------------------------------------
  2. //---------------------------------------------------------------------------
  3. #include "app32.h"
  4. #include <windowsx.h>
  5.  
  6. #include "resource.h"
  7.  
  8. #define MYWM_NOTIFYICON        (WM_APP+100)
  9.  
  10. //---------------------------------------------------------------------------
  11. // Global to everybody...
  12. HINSTANCE g_hinst;
  13.  
  14. #ifndef ARRAYSIZE
  15. #define ARRAYSIZE(a)    (sizeof(a)/sizeof(a[0]))
  16. #endif
  17.  
  18. struct _DLGITEMS
  19. {
  20.     DWORD dwStart;
  21.     UINT uNotify;
  22.     UINT uDelayID;
  23.     UINT uState1;
  24.     UINT uTip1;
  25.     UINT uState2;
  26.     UINT uTip2;
  27. } g_sDlgItems [] =
  28. {
  29.     {
  30.         0, IDC_NOTIFY1, IDC_DELAY1, IDC_STATE11, IDC_TIP11, IDC_STATE12, IDC_TIP12,
  31.     },
  32.     {
  33.         0, IDC_NOTIFY2, IDC_DELAY2, IDC_STATE21, IDC_TIP21, IDC_STATE22, IDC_TIP22,
  34.     },
  35.     {
  36.         0, IDC_NOTIFY3, IDC_DELAY3, IDC_STATE31, IDC_TIP31, IDC_STATE32, IDC_TIP32,
  37.     },
  38. } ;
  39.  
  40.  
  41. BOOL TrayMessage(HWND hDlg, DWORD dwMessage, UINT uID, HICON hIcon, PSTR pszTip)
  42. {
  43.         BOOL res;
  44.  
  45.     NOTIFYICONDATA tnd;
  46.  
  47.     tnd.cbSize        = sizeof(NOTIFYICONDATA);
  48.     tnd.hWnd        = hDlg;
  49.     tnd.uID            = uID;
  50.  
  51.     tnd.uFlags        = NIF_MESSAGE|NIF_ICON|NIF_TIP;
  52.     tnd.uCallbackMessage    = MYWM_NOTIFYICON;
  53.     tnd.hIcon        = hIcon;
  54.     if (pszTip)
  55.     {
  56.         lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
  57.     }
  58.     else
  59.     {
  60.         tnd.szTip[0] = '\0';
  61.     }
  62.  
  63.     res = Shell_NotifyIcon(dwMessage, &tnd);
  64.  
  65.     if (hIcon)
  66.         DestroyIcon(hIcon);
  67.  
  68.     return res;
  69. }
  70.  
  71.  
  72. LRESULT IconDrawItem(LPDRAWITEMSTRUCT lpdi)
  73. {
  74.     HICON hIcon;
  75.  
  76.     hIcon = (HICON)LoadImage(g_hinst, MAKEINTRESOURCE(lpdi->CtlID), IMAGE_ICON,
  77.         16, 16, 0);
  78.     if (!hIcon)
  79.     {
  80.         return(FALSE);
  81.     }
  82.  
  83.     DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon,
  84.         16, 16, 0, NULL, DI_NORMAL);
  85.  
  86.     return(TRUE);
  87. }
  88.  
  89.  
  90. void StateChange(HWND hDlg, UINT uIndex, UINT uSelect)
  91. {
  92.     UINT uState1, uState2;
  93.     HWND hwndIcon;
  94.     LPCSTR pszIDIcon;
  95.     UINT uTip;
  96.     char szTip[64];
  97.  
  98.     uState1 = g_sDlgItems[uIndex].uState1;
  99.     uState2 = g_sDlgItems[uIndex].uState2;
  100.  
  101.     // if !uSelect, find out which button is selected
  102.     if (!uSelect)
  103.     {
  104.         uSelect = IsDlgButtonChecked(hDlg, uState2) ? uState2 : uState1;
  105.     }
  106.     // if uSelect<0, find out shich button is NOT selected
  107.     else if ((int)uSelect < 0)
  108.     {
  109.         uSelect = IsDlgButtonChecked(hDlg, uState2) ? uState1 : uState2;
  110.     }
  111.  
  112.     CheckRadioButton(hDlg, uState1, uState2, uSelect);
  113.  
  114.     // If there is a tip specified, use it, otherwise use the default
  115.     uTip = uSelect==uState1
  116.         ? g_sDlgItems[uIndex].uTip1 : g_sDlgItems[uIndex].uTip2;
  117.     if (!GetDlgItemText(hDlg, uTip, szTip, sizeof(szTip))
  118.         && !LoadString(g_hinst, uSelect, szTip, sizeof(szTip)))
  119.     {
  120.         *szTip = '\0';
  121.     }
  122.  
  123.     // HACK: The ID of window after the radio button is the ID of the icon
  124.     hwndIcon = GetWindow(GetDlgItem(hDlg, uSelect), GW_HWNDNEXT);
  125.     pszIDIcon = MAKEINTRESOURCE(GetDlgCtrlID(hwndIcon));
  126.  
  127.     TrayMessage(hDlg, NIM_MODIFY, g_sDlgItems[uIndex].uNotify,
  128.         LoadImage(g_hinst, pszIDIcon, IMAGE_ICON, 16, 16, 0), szTip);
  129. }
  130.  
  131.  
  132. void NotifyDelete(HWND hDlg, UINT uIndex)
  133. {
  134.     TrayMessage(hDlg, NIM_DELETE, g_sDlgItems[uIndex].uNotify, NULL, NULL);
  135. }
  136.  
  137.  
  138. void NotifyAdd(HWND hDlg, UINT uIndex)
  139. {
  140.     TrayMessage(hDlg, NIM_ADD, g_sDlgItems[uIndex].uNotify, NULL, NULL);
  141.  
  142.     StateChange(hDlg, uIndex, 0);
  143. }
  144.  
  145.  
  146. void NotifyChange(HWND hDlg, UINT uIndex)
  147. {
  148.     UINT uDelay;
  149.     BOOL bTranslated;
  150.     BOOL bEnable;
  151.  
  152.     if (IsDlgButtonChecked(hDlg, g_sDlgItems[uIndex].uNotify))
  153.     {
  154.         uDelay = GetDlgItemInt(hDlg, g_sDlgItems[uIndex].uDelayID,
  155.             &bTranslated, FALSE);
  156.         if (uDelay)
  157.         {
  158.             g_sDlgItems[uIndex].dwStart = GetTickCount() + uDelay*60000;
  159.             SetTimer(hDlg, uIndex, 60000, NULL);
  160.         }
  161.         else
  162.         {
  163.             NotifyAdd(hDlg, uIndex);
  164.         }
  165.     }
  166.     else
  167.     {
  168.         NotifyDelete(hDlg, uIndex);
  169.     }
  170.  
  171.     for (uIndex=0, bEnable=FALSE; uIndex<ARRAYSIZE(g_sDlgItems); ++uIndex)
  172.     {
  173.         if (IsDlgButtonChecked(hDlg, g_sDlgItems[uIndex].uNotify))
  174.         {
  175.             bEnable = TRUE;
  176.         }
  177.     }
  178.     EnableWindow(GetDlgItem(hDlg, IDABORT), bEnable);
  179. }
  180.  
  181.  
  182. BOOL CALLBACK TestAppDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  183. {
  184.     switch (uMsg)
  185.     {
  186.     case WM_INITDIALOG:
  187.         break;
  188.  
  189.     case WM_DRAWITEM:
  190.         return(IconDrawItem((LPDRAWITEMSTRUCT)lParam));
  191.  
  192.     case WM_DESTROY:
  193.         NotifyDelete(hDlg, 0);
  194.         NotifyDelete(hDlg, 1);
  195.         NotifyDelete(hDlg, 2);
  196.         break;
  197.  
  198.     case WM_TIMER:
  199.         if (wParam >= ARRAYSIZE(g_sDlgItems))
  200.         {
  201.             break;
  202.         }
  203.  
  204.         if ((int)(GetTickCount() - g_sDlgItems[wParam].dwStart) < 0)
  205.         {
  206.             break;
  207.         }
  208.  
  209.         KillTimer(hDlg, wParam);
  210.         NotifyAdd(hDlg, wParam);
  211.         break;
  212.  
  213.     case WM_COMMAND:
  214.         switch (GET_WM_COMMAND_ID(wParam, lParam))
  215.         {
  216.         case IDCANCEL:
  217.             EndDialog(hDlg, TRUE);
  218.             break;
  219.  
  220.         case IDABORT:
  221.             ShowWindow(hDlg, SW_HIDE);
  222.             break;
  223.  
  224.         case IDC_NOTIFY1:
  225.             NotifyChange(hDlg, 0);
  226.             break;
  227.  
  228.         case IDC_NOTIFY2:
  229.             NotifyChange(hDlg, 1);
  230.             break;
  231.  
  232.         case IDC_NOTIFY3:
  233.             NotifyChange(hDlg, 2);
  234.             break;
  235.  
  236.         case IDC_STATE11:
  237.         case IDC_STATE12:
  238.             StateChange(hDlg, 0, GET_WM_COMMAND_ID(wParam, lParam));
  239.             break;
  240.  
  241.         case IDC_STATE21:
  242.         case IDC_STATE22:
  243.             StateChange(hDlg, 1, GET_WM_COMMAND_ID(wParam, lParam));
  244.             break;
  245.  
  246.         case IDC_STATE31:
  247.         case IDC_STATE32:
  248.             StateChange(hDlg, 2, GET_WM_COMMAND_ID(wParam, lParam));
  249.             break;
  250.         }
  251.         break;
  252.  
  253.     case MYWM_NOTIFYICON:
  254.         switch (lParam)
  255.         {
  256.         case WM_LBUTTONDOWN:
  257.             switch (wParam)
  258.             {
  259.             case IDC_NOTIFY1:
  260.                 StateChange(hDlg, 0, (UINT)-1);
  261.                 break;
  262.  
  263.             case IDC_NOTIFY2:
  264.                 StateChange(hDlg, 1, (UINT)-1);
  265.                 break;
  266.  
  267.             case IDC_NOTIFY3:
  268.                 StateChange(hDlg, 2, (UINT)-1);
  269.                 break;
  270.  
  271.             default:
  272.                 break;
  273.             }
  274.             break;
  275.  
  276.         case WM_RBUTTONDOWN:
  277.             ShowWindow(hDlg, SW_SHOW);
  278.             SetForegroundWindow(hDlg);    // make us come to the front
  279.             break;
  280.  
  281.         default:
  282.             break;
  283.         }
  284.         break;
  285.  
  286.     default:
  287.         return(FALSE);
  288.     }
  289.  
  290.     return(TRUE);
  291. }
  292.  
  293.  
  294. //---------------------------------------------------------------------------
  295. #pragma argsused
  296. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  297. {
  298.     OSVERSIONINFO osVer; // for GetVersionEx()
  299.  
  300.  
  301.     // Verify presence of Windows 95
  302.     osVer.dwOSVersionInfoSize = sizeof(osVer);
  303.     if (!GetVersionEx(&osVer))
  304.         return (FALSE);
  305.  
  306.     if (osVer.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
  307.     {
  308.     MessageBox(NULL, "This example requires Windows 95.",
  309.                  "Traynot Example", MB_OK );
  310.     return (FALSE);
  311.     }
  312.  
  313.  
  314.     g_hinst = hInstance;
  315.     DialogBox(hInstance, MAKEINTRESOURCE(IDD_APP32), NULL, TestAppDlgProc);
  316.     return(FALSE);
  317. }
  318.  
  319.