trayicon.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * ReactOS Task Manager
  3. *
  4. * trayicon.c
  5. *
  6. * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <windows.h>
  25. #include <commctrl.h>
  26. #include <winnt.h>
  27. #include <shellapi.h>
  28. #include "taskmgr.h"
  29. #include "perfdata.h"
  30. static HICON TrayIcon_GetProcessorUsageIcon(void)
  31. {
  32. HICON hTrayIcon = NULL;
  33. HDC hScreenDC = NULL;
  34. HDC hDC = NULL;
  35. HBITMAP hBitmap = NULL;
  36. HBITMAP hOldBitmap;
  37. HBITMAP hBitmapMask = NULL;
  38. ICONINFO iconInfo;
  39. ULONG ProcessorUsage;
  40. int nLinesToDraw;
  41. HBRUSH hBitmapBrush = NULL;
  42. RECT rc;
  43. /*
  44. * Get a handle to the screen DC
  45. */
  46. hScreenDC = GetDC(NULL);
  47. if (!hScreenDC)
  48. goto done;
  49. /*
  50. * Create our own DC from it
  51. */
  52. hDC = CreateCompatibleDC(hScreenDC);
  53. if (!hDC)
  54. goto done;
  55. /*
  56. * Load the bitmaps
  57. */
  58. hBitmap = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYICON));
  59. hBitmapMask = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYMASK));
  60. if (!hBitmap || !hBitmapMask)
  61. goto done;
  62. hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
  63. if (!hBitmapBrush)
  64. goto done;
  65. /*
  66. * Select the bitmap into our device context
  67. * so we can draw on it.
  68. */
  69. hOldBitmap = SelectObject(hDC, hBitmap);
  70. /*
  71. * Get the cpu usage
  72. */
  73. ProcessorUsage = PerfDataGetProcessorUsage();
  74. /*
  75. * Calculate how many lines to draw
  76. * since we have 11 rows of space
  77. * to draw the cpu usage instead of
  78. * just having 10.
  79. */
  80. nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
  81. SetRect(&rc, 3, 12 - nLinesToDraw, 13, 13);
  82. /*
  83. * Now draw the cpu usage
  84. */
  85. if (nLinesToDraw)
  86. FillRect(hDC, &rc, hBitmapBrush);
  87. /*
  88. * Now that we are done drawing put the
  89. * old bitmap back.
  90. */
  91. SelectObject(hDC, hOldBitmap);
  92. iconInfo.fIcon = TRUE;
  93. iconInfo.xHotspot = 0;
  94. iconInfo.yHotspot = 0;
  95. iconInfo.hbmMask = hBitmapMask;
  96. iconInfo.hbmColor = hBitmap;
  97. hTrayIcon = CreateIconIndirect(&iconInfo);
  98. done:
  99. /*
  100. * Cleanup
  101. */
  102. if (hScreenDC)
  103. ReleaseDC(NULL, hScreenDC);
  104. if (hDC)
  105. DeleteDC(hDC);
  106. if (hBitmapBrush)
  107. DeleteObject(hBitmapBrush);
  108. if (hBitmap)
  109. DeleteObject(hBitmap);
  110. if (hBitmapMask)
  111. DeleteObject(hBitmapMask);
  112. /*
  113. * Return the newly created tray icon (if successful)
  114. */
  115. return hTrayIcon;
  116. }
  117. BOOL TrayIcon_ShellAddTrayIcon(void)
  118. {
  119. NOTIFYICONDATAW nid;
  120. HICON hIcon = NULL;
  121. BOOL bRetVal;
  122. WCHAR wszCPU_Usage[255];
  123. LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, ARRAY_SIZE(wszCPU_Usage));
  124. memset(&nid, 0, sizeof(NOTIFYICONDATAW));
  125. hIcon = TrayIcon_GetProcessorUsageIcon();
  126. nid.cbSize = sizeof(NOTIFYICONDATAW);
  127. nid.hWnd = hMainWnd;
  128. nid.uID = 0;
  129. nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  130. nid.uCallbackMessage = WM_ONTRAYICON;
  131. nid.hIcon = hIcon;
  132. wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
  133. bRetVal = Shell_NotifyIconW(NIM_ADD, &nid);
  134. if (hIcon)
  135. DestroyIcon(hIcon);
  136. return bRetVal;
  137. }
  138. BOOL TrayIcon_ShellRemoveTrayIcon(void)
  139. {
  140. NOTIFYICONDATAW nid;
  141. BOOL bRetVal;
  142. memset(&nid, 0, sizeof(NOTIFYICONDATAW));
  143. nid.cbSize = sizeof(NOTIFYICONDATAW);
  144. nid.hWnd = hMainWnd;
  145. nid.uID = 0;
  146. nid.uFlags = 0;
  147. nid.uCallbackMessage = WM_ONTRAYICON;
  148. bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid);
  149. return bRetVal;
  150. }
  151. BOOL TrayIcon_ShellUpdateTrayIcon(void)
  152. {
  153. NOTIFYICONDATAW nid;
  154. HICON hIcon = NULL;
  155. BOOL bRetVal;
  156. WCHAR wszCPU_Usage[255];
  157. LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, ARRAY_SIZE(wszCPU_Usage));
  158. memset(&nid, 0, sizeof(NOTIFYICONDATAW));
  159. hIcon = TrayIcon_GetProcessorUsageIcon();
  160. nid.cbSize = sizeof(NOTIFYICONDATAW);
  161. nid.hWnd = hMainWnd;
  162. nid.uID = 0;
  163. nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  164. nid.uCallbackMessage = WM_ONTRAYICON;
  165. nid.hIcon = hIcon;
  166. wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
  167. bRetVal = Shell_NotifyIconW(NIM_MODIFY, &nid);
  168. if (hIcon)
  169. DestroyIcon(hIcon);
  170. return bRetVal;
  171. }