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

View file

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

View file

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