ReactOS 0.4.15-dev-8428-g6910fa6
trayicon.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Task Manager
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Tray Icon.
5 * COPYRIGHT: Copyright 1999-2001 Brian Palmer <brianp@reactos.org>
6 * Copyright 2005 Klemens Friedl <frik85@reactos.at>
7 */
8
9#include "precomp.h"
10
11static HICON
13 _In_ ULONG CpuUsage)
14{
15 HICON hTrayIcon = NULL;
16 HDC hScreenDC = NULL;
17 HDC hDC = NULL;
19 HBITMAP hOldBitmap = NULL;
20 HBITMAP hBitmapMask = NULL;
21 ICONINFO iconInfo;
22 int nLinesToDraw;
23 HBRUSH hBitmapBrush = NULL;
24 RECT rc;
25
26 /* Get a handle to the screen DC */
27 hScreenDC = GetDC(NULL);
28 if (!hScreenDC)
29 goto done;
30
31 /* Create our own DC from it */
32 hDC = CreateCompatibleDC(hScreenDC);
33 if (!hDC)
34 goto done;
35
36 /*
37 * Load the bitmaps
38 */
41 if (!hBitmap || !hBitmapMask)
42 goto done;
43
44 hBitmapBrush = CreateSolidBrush(RGB(0, 255, 0));
45 if (!hBitmapBrush)
46 goto done;
47
48 /*
49 * Select the bitmap into our device context
50 * so we can draw on it.
51 */
52 hOldBitmap = SelectObject(hDC, hBitmap);
53
54 /*
55 * Calculate how many lines to draw
56 * since we have 11 rows of space
57 * to draw the cpu usage instead of
58 * just having 10.
59 */
60 nLinesToDraw = (CpuUsage + (CpuUsage / 10)) / 11;
61 rc.left = 3;
62 rc.top = 12 - nLinesToDraw;
63 rc.right = 13;
64 rc.bottom = 13;
65
66 /* Now draw the cpu usage */
67 if (nLinesToDraw)
68 FillRect(hDC, &rc, hBitmapBrush);
69
70 /*
71 * Now that we are done drawing put the
72 * old bitmap back.
73 */
74 hBitmap = SelectObject(hDC, hOldBitmap);
75 hOldBitmap = NULL;
76
77 iconInfo.fIcon = TRUE;
78 iconInfo.hbmMask = hBitmapMask;
79 iconInfo.hbmColor = hBitmap;
80
81 hTrayIcon = CreateIconIndirect(&iconInfo);
82
83done:
84 /*
85 * Cleanup
86 */
87 if (hScreenDC)
88 ReleaseDC(NULL, hScreenDC);
89 if (hDC)
91 if (hBitmapBrush)
92 DeleteObject(hBitmapBrush);
93 if (hBitmap)
95 if (hBitmapMask)
96 DeleteObject(hBitmapMask);
97
98 /* Return the newly created tray icon (if successful) */
99 return hTrayIcon;
100}
101
102static BOOL
104 _In_ DWORD dwMessage)
105{
106 static WCHAR szMsg[64] = L"";
107
109 ULONG CpuUsage;
110 HICON hIcon = NULL;
111 BOOL bRetVal;
112
113 if (!*szMsg)
115
116 ZeroMemory(&nid, sizeof(nid));
117
118 CpuUsage = PerfDataGetProcessorUsage();
120
121 nid.cbSize = sizeof(nid);
122 nid.hWnd = hMainWnd;
123 nid.uID = 0;
126 nid.hIcon = hIcon;
127
128 StringCchPrintfW(nid.szTip, _countof(nid.szTip), szMsg, CpuUsage);
129
130 bRetVal = Shell_NotifyIconW(dwMessage, &nid);
131
132 if (hIcon)
134
135 return bRetVal;
136}
137
139{
140 return TrayIcon_Update(NIM_ADD);
141}
142
144{
146
147 ZeroMemory(&nid, sizeof(nid));
148
149 nid.cbSize = sizeof(nid);
150 nid.hWnd = hMainWnd;
151 nid.uID = 0;
152 nid.uFlags = 0;
154
156}
157
159{
161}
static HDC hDC
Definition: 3dtext.c:33
#define IDB_TRAYMASK
Definition: resource.h:33
#define IDB_TRAYICON
Definition: resource.h:34
#define IDS_MSG_TRAYICONCPUUSAGE
Definition: resource.h:257
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
static HBITMAP hBitmap
Definition: timezone.c:26
#define RGB(r, g, b)
Definition: precomp.h:71
HINSTANCE hInst
Definition: dxdiag.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
pKey DeleteObject()
HWND hMainWnd
Definition: magnifier.c:32
NOTIFYICONDATA nid
Definition: magnifier.c:44
static HBITMAP
Definition: button.c:44
static HDC
Definition: imagelist.c:92
static HICON
Definition: imagelist.c:84
#define _In_
Definition: ms_sal.h:308
HICON hIcon
Definition: msconfig.c:44
#define L(x)
Definition: ntvdm.h:50
ULONG PerfDataGetProcessorUsage(void)
Definition: perfdata.c:455
#define NIM_DELETE
Definition: shellapi.h:96
#define NIM_MODIFY
Definition: shellapi.h:95
#define NIF_ICON
Definition: shellapi.h:106
#define NIF_MESSAGE
Definition: shellapi.h:105
#define NIM_ADD
Definition: shellapi.h:94
#define NIF_TIP
Definition: shellapi.h:107
#define _countof(array)
Definition: sndvol32.h:70
STRSAFEAPI StringCchPrintfW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:530
BOOL fIcon
Definition: winuser.h:3123
HBITMAP hbmColor
Definition: winuser.h:3127
HBITMAP hbmMask
Definition: winuser.h:3126
UINT uCallbackMessage
Definition: shellapi.h:231
CHAR szTip[128]
Definition: shellapi.h:237
LONG right
Definition: windef.h:308
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
BOOL WINAPI Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW pnid)
Definition: systray.cpp:128
static BOOL TrayIcon_Update(_In_ DWORD dwMessage)
Definition: trayicon.c:103
BOOL TrayIcon_RemoveIcon(VOID)
Definition: trayicon.c:143
BOOL TrayIcon_UpdateIcon(VOID)
Definition: trayicon.c:158
BOOL TrayIcon_AddIcon(VOID)
Definition: trayicon.c:138
static HICON TrayIcon_GetProcessorUsageIcon(_In_ ULONG CpuUsage)
Definition: trayicon.c:12
#define WM_ONTRAYICON
Definition: trayicon.h:11
uint32_t ULONG
Definition: typedefs.h:59
#define ZeroMemory
Definition: winbase.h:1712
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1546
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
BOOL WINAPI DeleteDC(_In_ HDC)
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
HICON WINAPI CreateIconIndirect(_In_ PICONINFO)
Definition: cursoricon.c:2625
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
HDC WINAPI GetDC(_In_opt_ HWND)
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
HBITMAP WINAPI LoadBitmapW(_In_opt_ HINSTANCE, _In_ LPCWSTR)
Definition: cursoricon.c:2207
BOOL WINAPI DestroyIcon(_In_ HICON)
Definition: cursoricon.c:2097
__wchar_t WCHAR
Definition: xmlstorage.h:180