From 8d04988c121db7015af74dfd668dd1be58655a1d Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 5 May 2012 18:53:33 +0300 Subject: [PATCH] Changed tray notifications to use 32x32 icon on Vista+ --- Library/DialogAbout.cpp | 4 ++-- Library/DialogManage.cpp | 2 +- Library/Litestep.cpp | 30 ++++++++++++++++++++++++++++++ Library/Litestep.h | 2 ++ Library/TrayWindow.cpp | 29 +++++++++-------------------- Library/TrayWindow.h | 5 ++--- 6 files changed, 46 insertions(+), 26 deletions(-) diff --git a/Library/DialogAbout.cpp b/Library/DialogAbout.cpp index f085d98d..41021960 100644 --- a/Library/DialogAbout.cpp +++ b/Library/DialogAbout.cpp @@ -257,7 +257,7 @@ INT_PTR CDialogAbout::OnInitDialog(WPARAM wParam, LPARAM lParam) HWND item = GetDlgItem(m_Window, IDCLOSE); SendMessage(m_Window, WM_NEXTDLGCTL, (WPARAM)item, TRUE); - HICON hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_RAINMETER), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED); + HICON hIcon = GetIcon(IDI_RAINMETER); SendMessage(m_Window, WM_SETICON, ICON_SMALL, (LPARAM)hIcon); if (*GetString(ID_STR_ISRTL) == L'1') @@ -1084,7 +1084,7 @@ void CDialogAbout::CTabVersion::Initialize() m_Initialized = true; HWND item = GetDlgItem(m_Window, IDC_ABOUTVERSION_RAINMETER_ICON); - HICON icon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_RAINMETER), IMAGE_ICON, 32, 32, LR_SHARED); + HICON icon = GetIcon(IDI_RAINMETER, true); Static_SetIcon(item, icon); item = GetDlgItem(m_Window, IDC_ABOUTVERSION_VERSION_TEXT); diff --git a/Library/DialogManage.cpp b/Library/DialogManage.cpp index a71f2739..12524a63 100644 --- a/Library/DialogManage.cpp +++ b/Library/DialogManage.cpp @@ -232,7 +232,7 @@ INT_PTR CDialogManage::OnInitDialog(WPARAM wParam, LPARAM lParam) HWND item = GetDlgItem(m_Window, IDCLOSE); SendMessage(m_Window, WM_NEXTDLGCTL, (WPARAM)item, TRUE); - HICON hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_RAINMETER), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED); + HICON hIcon = GetIcon(IDI_RAINMETER); SendMessage(m_Window, WM_SETICON, ICON_SMALL, (LPARAM)hIcon); if (*GetString(ID_STR_ISRTL) == L'1') diff --git a/Library/Litestep.cpp b/Library/Litestep.cpp index 810551a4..42e76a70 100644 --- a/Library/Litestep.cpp +++ b/Library/Litestep.cpp @@ -337,6 +337,36 @@ std::wstring GetFormattedString(UINT id, ...) return tmpSz; } +HICON GetIcon(UINT id, bool large) +{ + typedef HRESULT (WINAPI * FPLOADICONMETRIC)(HINSTANCE hinst, PCWSTR pszName, int lims, HICON* phico); + + HINSTANCE hExe = GetModuleHandle(NULL); + HINSTANCE hComctl = GetModuleHandle(L"Comctl32"); + if (hComctl) + { + // Try LoadIconMetric for better quality with high DPI + FPLOADICONMETRIC loadIconMetric = (FPLOADICONMETRIC)GetProcAddress(hComctl, "LoadIconMetric"); + if (loadIconMetric) + { + HICON icon; + HRESULT hr = loadIconMetric(hExe, MAKEINTRESOURCE(id), large ? LIM_LARGE : LIM_SMALL, &icon); + if (SUCCEEDED(hr)) + { + return icon; + } + } + } + + return (HICON)LoadImage( + hExe, + MAKEINTRESOURCE(id), + IMAGE_ICON, + GetSystemMetrics(large ? SM_CXICON : SM_CXSMICON), + GetSystemMetrics(large ? SM_CYICON : SM_CYSMICON), + LR_SHARED); +} + void RmNullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved) { // Do nothing. diff --git a/Library/Litestep.h b/Library/Litestep.h index 3ff52d3e..60bdaac9 100644 --- a/Library/Litestep.h +++ b/Library/Litestep.h @@ -54,6 +54,8 @@ void RunCommand(HWND Owner, LPCTSTR szCommand, int nShowCmd, bool asAdmin = fals WCHAR* GetString(UINT id); std::wstring GetFormattedString(UINT id, ...); +HICON GetIcon(UINT id, bool large = false); + void RmNullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved); #endif diff --git a/Library/TrayWindow.cpp b/Library/TrayWindow.cpp index e871a484..eb9c593b 100644 --- a/Library/TrayWindow.cpp +++ b/Library/TrayWindow.cpp @@ -24,6 +24,7 @@ #include "Rainmeter.h" #include "DialogAbout.h" #include "DialogManage.h" +#include "System.h" #include "Error.h" #include "RainmeterQuery.h" #include "resource.h" @@ -86,7 +87,7 @@ void CTrayWindow::Initialize() wc.lpfnWndProc = (WNDPROC)WndProc; wc.hInstance = Rainmeter->GetInstance(); wc.lpszClassName = L"RainmeterTrayClass"; - wc.hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_RAINMETER), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), LR_SHARED); + wc.hIcon = GetIcon(IDI_RAINMETER); RegisterClass(&wc); @@ -260,24 +261,7 @@ HICON CTrayWindow::CreateTrayIcon(double value) } // Return the default icon if there is no valid measure - HINSTANCE hExe = GetModuleHandle(NULL); - HINSTANCE hComctl = GetModuleHandle(L"Comctl32"); - if (hComctl) - { - // Try LoadIconMetric for better quality with high DPI - FPLOADICONMETRIC loadIconMetric = (FPLOADICONMETRIC)GetProcAddress(hComctl, "LoadIconMetric"); - if (loadIconMetric) - { - HICON icon; - HRESULT hr = loadIconMetric(hExe, MAKEINTRESOURCE(IDI_TRAY), LIM_SMALL, &icon); - if (SUCCEEDED(hr)) - { - return icon; - } - } - } - - return (HICON)LoadImage(hExe, MAKEINTRESOURCE(IDI_TRAY), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED); + return GetIcon(IDI_TRAY); } void CTrayWindow::ShowNotification(TRAY_NOTIFICATION id, const WCHAR* title, const WCHAR* text) @@ -290,10 +274,15 @@ void CTrayWindow::ShowNotification(TRAY_NOTIFICATION id, const WCHAR* title, con nid.uFlags = NIF_INFO; nid.uTimeout = 30000; nid.dwInfoFlags = NIIF_USER; - nid.hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_RAINMETER), IMAGE_ICON, 32, 32, LR_SHARED); wcsncpy_s(nid.szInfoTitle, title, _TRUNCATE); wcsncpy_s(nid.szInfo, text, _TRUNCATE); + if (CSystem::GetOSPlatform() > OSPLATFORM_VISTA) + { + nid.dwInfoFlags |= NIIF_LARGE_ICON; + nid.hBalloonIcon = GetIcon(IDI_RAINMETER, true); + } + if (Shell_NotifyIcon(NIM_MODIFY, &nid)) { m_Notification = id; diff --git a/Library/TrayWindow.h b/Library/TrayWindow.h index e6409279..e669f8b8 100644 --- a/Library/TrayWindow.h +++ b/Library/TrayWindow.h @@ -23,8 +23,6 @@ #include #include -typedef HRESULT (WINAPI * FPLOADICONMETRIC)(HINSTANCE hinst, PCWSTR pszName, int lims, HICON* phico); - #define WM_TRAY_NOTIFYICON WM_USER + 101 #define TRAYICON_SIZE 16 @@ -69,11 +67,12 @@ private: void ModifyTrayIcon(double value); HICON CreateTrayIcon(double value); + HICON LoadResourceIcon(LPCWSTR name, bool large = false); + void ShowNotification(TRAY_NOTIFICATION id, const WCHAR* title, const WCHAR* text); HICON m_Icon; HWND m_Window; - HINSTANCE m_Instance; CMeasure* m_Measure; TRAY_METER_TYPE m_MeterType;