home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2000 January / PCW0001.ISO / software / hw / pc2000 / junkbust.exe / w32taskbar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-21  |  3.3 KB  |  170 lines

  1. /* Functions for creating, setting and destroying the workspace tray icon
  2.  *
  3.  * Written and copyright 1999 Adam Lock <locka@iol.ie>
  4.  *
  5.  * Distributed under the GNU General Public License; see the README file.
  6.  * This code comes with NO WARRANTY.
  7.  */
  8. #include "stdio.h"
  9.  
  10. #include "windows.h"
  11.  
  12. #include "w32taskbar.h"
  13. #include "w32res.h"
  14. #include "w32log.h"
  15.  
  16. #define WM_TRAYMSG WM_USER+1
  17.  
  18. static HMENU g_hmenuTray;
  19. static HWND g_hwndTrayX;
  20.  
  21. static LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  22.  
  23. /*
  24.  * Creates and returns the invisible window responsible for processing tray messages
  25.  */
  26. HWND CreateTrayWindow(HINSTANCE hInstance)
  27. {
  28.     WNDCLASS wc;
  29.     static const char *szWndName = "JunkbusterTrayWindow";
  30.  
  31.     wc.style            = 0;
  32.     wc.lpfnWndProc        = TrayProc;
  33.     wc.cbClsExtra        = 0;
  34.     wc.cbWndExtra        = 0;
  35.     wc.hInstance        = hInstance;
  36.     wc.hIcon            = 0;
  37.     wc.hCursor            = 0;
  38.     wc.hbrBackground    = 0;
  39.     wc.lpszMenuName        = 0;
  40.     wc.lpszClassName    = szWndName;
  41.  
  42.     RegisterClass(&wc);
  43.  
  44.     g_hwndTrayX = CreateWindow(szWndName, szWndName,
  45.         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  46.         CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
  47.  
  48.     ShowWindow(g_hwndTrayX, SW_HIDE);
  49.     UpdateWindow(g_hwndTrayX);
  50.  
  51.     g_hmenuTray = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
  52.  
  53.     return g_hwndTrayX;
  54. }
  55.  
  56.  
  57. /*
  58.  * Sets the tray icon to the specified shape
  59.  */
  60. BOOL TraySetIcon(HWND hwnd, UINT uID, HICON hicon)
  61. {
  62.     BOOL bSuccess;
  63.     NOTIFYICONDATA nid;
  64.  
  65.     memset(&nid, 0, sizeof(nid));
  66.  
  67.     nid.cbSize = sizeof(nid);
  68.     nid.hWnd = hwnd;
  69.     nid.uID = uID;
  70.     nid.uFlags = NIF_ICON;
  71.     nid.uCallbackMessage = 0;
  72.     nid.hIcon = hicon;
  73.  
  74.     bSuccess = Shell_NotifyIcon(NIM_MODIFY, &nid);
  75.  
  76.     return bSuccess;
  77. }
  78.  
  79.  
  80. /*
  81.  * Adds a tray icon
  82.  */
  83. BOOL TrayAddIcon(HWND hwnd, UINT uID, HICON hicon, const char *pszToolTip)
  84. {
  85.     BOOL bSuccess;
  86.     NOTIFYICONDATA nid;
  87.  
  88.     memset(&nid, 0, sizeof(nid));
  89.  
  90.     nid.cbSize = sizeof(nid);
  91.     nid.hWnd = hwnd;
  92.     nid.uID = uID;
  93.     nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  94.     nid.uCallbackMessage = WM_TRAYMSG;
  95.     nid.hIcon = hicon;
  96.  
  97.     if (pszToolTip)
  98.     {
  99.         strcpy(nid.szTip, pszToolTip);
  100.     }
  101.  
  102.     bSuccess = Shell_NotifyIcon(NIM_ADD, &nid);
  103.  
  104.     return bSuccess;
  105. }
  106.  
  107.  
  108. /*
  109.  * Deletes a tray icon
  110.  */
  111. BOOL TrayDeleteIcon(HWND hwnd, UINT uID)
  112. {
  113.     BOOL bSuccess;
  114.     NOTIFYICONDATA nid;
  115.  
  116.     memset(&nid, 0, sizeof(nid));
  117.  
  118.     nid.cbSize = sizeof(nid);
  119.     nid.hWnd = hwnd;
  120.     nid.uID = uID;
  121.  
  122.     bSuccess = Shell_NotifyIcon(NIM_DELETE, &nid);
  123.  
  124.     return bSuccess;
  125. }
  126.  
  127.  
  128. /*
  129.  * Call back procedure processes tray messages 
  130.  */
  131. LRESULT CALLBACK TrayProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  132. {
  133.     switch(msg)
  134.     {
  135.     case WM_CREATE:
  136.         return 0;
  137.  
  138.     case WM_CLOSE:
  139.         PostQuitMessage(0);    
  140.         return 0;
  141.  
  142.     case WM_TRAYMSG:
  143.         {
  144.             UINT uID = (UINT) wParam;
  145.             UINT uMouseMsg = (UINT) lParam;
  146.  
  147.             if (uMouseMsg == WM_RBUTTONDOWN)
  148.             {
  149.                 POINT pt;
  150.                 HMENU hmenu = GetSubMenu(g_hmenuTray,0);
  151.                 GetCursorPos(&pt);
  152.                 SetForegroundWindow(g_hwndLogFrame);
  153.                 TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, 0, g_hwndLogFrame, NULL);
  154.                 PostMessage(g_hwndLogFrame, WM_NULL, 0, 0 ) ;
  155.             }
  156.             else if (uMouseMsg == WM_LBUTTONDBLCLK)
  157.             {
  158.                 ShowLogWindow(TRUE);
  159.             }
  160.         }
  161.         return 0;
  162.     
  163.     default:
  164.         /* DO NOTHING */
  165.         break;
  166.     }
  167.  
  168.     return DefWindowProc(hwnd, msg, wParam, lParam);
  169. }
  170.