iphlpapi: Implement ConvertInterfaceAliasToLuid().
[wine.git] / programs / taskmgr / trayicon.c
blob6b6956acadfc065b94d2533748542290b7e2fc27
1 /*
2 * ReactOS Task Manager
4 * trayicon.c
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
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.
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.
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
23 #include <stdio.h>
24 #include <stdlib.h>
26 #include <windows.h>
27 #include <commctrl.h>
28 #include <winnt.h>
29 #include <shellapi.h>
31 #include "taskmgr.h"
32 #include "perfdata.h"
34 static HICON TrayIcon_GetProcessorUsageIcon(void)
36 HICON hTrayIcon = NULL;
37 HDC hScreenDC = NULL;
38 HDC hDC = NULL;
39 HBITMAP hBitmap = NULL;
40 HBITMAP hOldBitmap;
41 HBITMAP hBitmapMask = NULL;
42 ICONINFO iconInfo;
43 ULONG ProcessorUsage;
44 int nLinesToDraw;
45 HBRUSH hBitmapBrush = NULL;
46 RECT rc;
49 * Get a handle to the screen DC
51 hScreenDC = GetDC(NULL);
52 if (!hScreenDC)
53 goto done;
56 * Create our own DC from it
58 hDC = CreateCompatibleDC(hScreenDC);
59 if (!hDC)
60 goto done;
63 * Load the bitmaps
65 hBitmap = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYICON));
66 hBitmapMask = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_TRAYMASK));
67 if (!hBitmap || !hBitmapMask)
68 goto done;
70 hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
71 if (!hBitmapBrush)
72 goto done;
75 * Select the bitmap into our device context
76 * so we can draw on it.
78 hOldBitmap = SelectObject(hDC, hBitmap);
81 * Get the cpu usage
83 ProcessorUsage = PerfDataGetProcessorUsage();
86 * Calculate how many lines to draw
87 * since we have 11 rows of space
88 * to draw the cpu usage instead of
89 * just having 10.
91 nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11;
92 SetRect(&rc, 3, 12 - nLinesToDraw, 13, 13);
95 * Now draw the cpu usage
97 if (nLinesToDraw)
98 FillRect(hDC, &rc, hBitmapBrush);
101 * Now that we are done drawing put the
102 * old bitmap back.
104 SelectObject(hDC, hOldBitmap);
106 iconInfo.fIcon = TRUE;
107 iconInfo.xHotspot = 0;
108 iconInfo.yHotspot = 0;
109 iconInfo.hbmMask = hBitmapMask;
110 iconInfo.hbmColor = hBitmap;
112 hTrayIcon = CreateIconIndirect(&iconInfo);
114 done:
116 * Cleanup
118 if (hScreenDC)
119 ReleaseDC(NULL, hScreenDC);
120 if (hDC)
121 DeleteDC(hDC);
122 if (hBitmapBrush)
123 DeleteObject(hBitmapBrush);
124 if (hBitmap)
125 DeleteObject(hBitmap);
126 if (hBitmapMask)
127 DeleteObject(hBitmapMask);
130 * Return the newly created tray icon (if successful)
132 return hTrayIcon;
135 BOOL TrayIcon_ShellAddTrayIcon(void)
137 NOTIFYICONDATAW nid;
138 HICON hIcon = NULL;
139 BOOL bRetVal;
140 WCHAR wszCPU_Usage[255];
142 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, ARRAY_SIZE(wszCPU_Usage));
144 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
146 hIcon = TrayIcon_GetProcessorUsageIcon();
148 nid.cbSize = sizeof(NOTIFYICONDATAW);
149 nid.hWnd = hMainWnd;
150 nid.uID = 0;
151 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
152 nid.uCallbackMessage = WM_ONTRAYICON;
153 nid.hIcon = hIcon;
154 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
156 bRetVal = Shell_NotifyIconW(NIM_ADD, &nid);
158 if (hIcon)
159 DestroyIcon(hIcon);
161 return bRetVal;
164 BOOL TrayIcon_ShellRemoveTrayIcon(void)
166 NOTIFYICONDATAW nid;
167 BOOL bRetVal;
169 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
171 nid.cbSize = sizeof(NOTIFYICONDATAW);
172 nid.hWnd = hMainWnd;
173 nid.uID = 0;
174 nid.uFlags = 0;
175 nid.uCallbackMessage = WM_ONTRAYICON;
177 bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid);
179 return bRetVal;
182 BOOL TrayIcon_ShellUpdateTrayIcon(void)
184 NOTIFYICONDATAW nid;
185 HICON hIcon = NULL;
186 BOOL bRetVal;
187 WCHAR wszCPU_Usage[255];
189 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, ARRAY_SIZE(wszCPU_Usage));
191 memset(&nid, 0, sizeof(NOTIFYICONDATAW));
193 hIcon = TrayIcon_GetProcessorUsageIcon();
195 nid.cbSize = sizeof(NOTIFYICONDATAW);
196 nid.hWnd = hMainWnd;
197 nid.uID = 0;
198 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
199 nid.uCallbackMessage = WM_ONTRAYICON;
200 nid.hIcon = hIcon;
201 wsprintfW(nid.szTip, wszCPU_Usage, PerfDataGetProcessorUsage());
203 bRetVal = Shell_NotifyIconW(NIM_MODIFY, &nid);
205 if (hIcon)
206 DestroyIcon(hIcon);
208 return bRetVal;