[TASKMGR] Simplify tray icon code.

This commit is contained in:
Hermès Bélusca-Maïto 2021-12-21 00:18:51 +01:00
parent 0b4c8bdd1c
commit 6e77747b30
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
3 changed files with 39 additions and 59 deletions

View file

@ -475,7 +475,7 @@ TaskManagerWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
case WM_DESTROY: case WM_DESTROY:
ShowWindow(hDlg, SW_HIDE); ShowWindow(hDlg, SW_HIDE);
TrayIcon_ShellRemoveTrayIcon(); TrayIcon_RemoveIcon();
wp.length = sizeof(WINDOWPLACEMENT); wp.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hDlg, &wp); GetWindowPlacement(hDlg, &wp);
TaskManagerSettings.Left = wp.rcNormalPosition.left; TaskManagerSettings.Left = wp.rcNormalPosition.left;
@ -498,7 +498,7 @@ TaskManagerWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
RefreshApplicationPage(); RefreshApplicationPage();
RefreshProcessPage(); RefreshProcessPage();
RefreshPerformancePage(); RefreshPerformancePage();
TrayIcon_ShellUpdateTrayIcon(); TrayIcon_UpdateIcon();
break; break;
case WM_ENTERMENULOOP: case WM_ENTERMENULOOP:
@ -763,7 +763,7 @@ BOOL OnCreate(HWND hWnd)
RefreshProcessPage(); RefreshProcessPage();
RefreshPerformancePage(); RefreshPerformancePage();
TrayIcon_ShellAddTrayIcon(); TrayIcon_AddIcon();
return TRUE; return TRUE;
} }

View file

@ -8,7 +8,9 @@
#include "precomp.h" #include "precomp.h"
HICON TrayIcon_GetProcessorUsageIcon(void) static HICON
TrayIcon_GetProcessorUsageIcon(
_In_ ULONG CpuUsage)
{ {
HICON hTrayIcon = NULL; HICON hTrayIcon = NULL;
HDC hScreenDC = NULL; HDC hScreenDC = NULL;
@ -17,7 +19,6 @@ HICON TrayIcon_GetProcessorUsageIcon(void)
HBITMAP hOldBitmap = NULL; HBITMAP hOldBitmap = NULL;
HBITMAP hBitmapMask = NULL; HBITMAP hBitmapMask = NULL;
ICONINFO iconInfo; ICONINFO iconInfo;
ULONG ProcessorUsage;
int nLinesToDraw; int nLinesToDraw;
HBRUSH hBitmapBrush = NULL; HBRUSH hBitmapBrush = NULL;
RECT rc; RECT rc;
@ -50,18 +51,13 @@ HICON TrayIcon_GetProcessorUsageIcon(void)
*/ */
hOldBitmap = SelectObject(hDC, hBitmap); hOldBitmap = SelectObject(hDC, hBitmap);
/*
* Get the cpu usage
*/
ProcessorUsage = PerfDataGetProcessorUsage();
/* /*
* Calculate how many lines to draw * Calculate how many lines to draw
* since we have 11 rows of space * since we have 11 rows of space
* to draw the cpu usage instead of * to draw the cpu usage instead of
* just having 10. * just having 10.
*/ */
nLinesToDraw = (ProcessorUsage + (ProcessorUsage / 10)) / 11; nLinesToDraw = (CpuUsage + (CpuUsage / 10)) / 11;
rc.left = 3; rc.left = 3;
rc.top = 12 - nLinesToDraw; rc.top = 12 - nLinesToDraw;
rc.right = 13; rc.right = 13;
@ -103,29 +99,35 @@ done:
return hTrayIcon; return hTrayIcon;
} }
BOOL TrayIcon_ShellAddTrayIcon(void) static BOOL
TrayIcon_Update(
_In_ DWORD dwMessage)
{ {
static WCHAR szMsg[64] = L"";
NOTIFYICONDATAW nid; NOTIFYICONDATAW nid;
HICON hIcon = NULL; ULONG CpuUsage;
BOOL bRetVal; HICON hIcon = NULL;
WCHAR szMsg[64]; BOOL bRetVal;
memset(&nid, 0, sizeof(NOTIFYICONDATAW)); if (!*szMsg)
LoadStringW(hInst, IDS_MSG_TRAYICONCPUUSAGE, szMsg, ARRAYSIZE(szMsg));
hIcon = TrayIcon_GetProcessorUsageIcon(); ZeroMemory(&nid, sizeof(nid));
nid.cbSize = sizeof(NOTIFYICONDATAW); CpuUsage = PerfDataGetProcessorUsage();
hIcon = TrayIcon_GetProcessorUsageIcon(CpuUsage);
nid.cbSize = sizeof(nid);
nid.hWnd = hMainWnd; nid.hWnd = hMainWnd;
nid.uID = 0; nid.uID = 0;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_ONTRAYICON; nid.uCallbackMessage = WM_ONTRAYICON;
nid.hIcon = hIcon; nid.hIcon = hIcon;
wsprintfW(nid.szTip, szMsg, CpuUsage);
LoadStringW( GetModuleHandleW(NULL), IDS_MSG_TRAYICONCPUUSAGE, szMsg, sizeof(szMsg) / sizeof(szMsg[0])); bRetVal = Shell_NotifyIconW(dwMessage, &nid);
wsprintfW(nid.szTip, szMsg, PerfDataGetProcessorUsage());
bRetVal = Shell_NotifyIconW(NIM_ADD, &nid);
if (hIcon) if (hIcon)
DestroyIcon(hIcon); DestroyIcon(hIcon);
@ -133,48 +135,27 @@ BOOL TrayIcon_ShellAddTrayIcon(void)
return bRetVal; return bRetVal;
} }
BOOL TrayIcon_ShellRemoveTrayIcon(void) BOOL TrayIcon_AddIcon(VOID)
{
return TrayIcon_Update(NIM_ADD);
}
BOOL TrayIcon_RemoveIcon(VOID)
{ {
NOTIFYICONDATAW nid; NOTIFYICONDATAW nid;
BOOL bRetVal;
memset(&nid, 0, sizeof(NOTIFYICONDATAW)); ZeroMemory(&nid, sizeof(nid));
nid.cbSize = sizeof(NOTIFYICONDATAW); nid.cbSize = sizeof(nid);
nid.hWnd = hMainWnd; nid.hWnd = hMainWnd;
nid.uID = 0; nid.uID = 0;
nid.uFlags = 0; nid.uFlags = 0;
nid.uCallbackMessage = WM_ONTRAYICON; nid.uCallbackMessage = WM_ONTRAYICON;
bRetVal = Shell_NotifyIconW(NIM_DELETE, &nid); return Shell_NotifyIconW(NIM_DELETE, &nid);
return bRetVal;
} }
BOOL TrayIcon_ShellUpdateTrayIcon(void) BOOL TrayIcon_UpdateIcon(VOID)
{ {
NOTIFYICONDATAW nid; return TrayIcon_Update(NIM_MODIFY);
HICON hIcon = NULL;
BOOL bRetVal;
WCHAR szTemp[64];
memset(&nid, 0, sizeof(NOTIFYICONDATAW));
hIcon = TrayIcon_GetProcessorUsageIcon();
nid.cbSize = sizeof(NOTIFYICONDATAW);
nid.hWnd = hMainWnd;
nid.uID = 0;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_ONTRAYICON;
nid.hIcon = hIcon;
LoadStringW(hInst, IDS_MSG_TRAYICONCPUUSAGE, szTemp, sizeof(szTemp)/sizeof(szTemp[0]));
wsprintfW(nid.szTip, szTemp, PerfDataGetProcessorUsage());
bRetVal = Shell_NotifyIconW(NIM_MODIFY, &nid);
if (hIcon)
DestroyIcon(hIcon);
return bRetVal;
} }

View file

@ -8,9 +8,8 @@
#pragma once #pragma once
#define WM_ONTRAYICON WM_USER + 5 #define WM_ONTRAYICON (WM_USER + 5)
HICON TrayIcon_GetProcessorUsageIcon(void); BOOL TrayIcon_AddIcon(VOID);
BOOL TrayIcon_ShellAddTrayIcon(void); BOOL TrayIcon_RemoveIcon(VOID);
BOOL TrayIcon_ShellRemoveTrayIcon(void); BOOL TrayIcon_UpdateIcon(VOID);
BOOL TrayIcon_ShellUpdateTrayIcon(void);