From 2f2ddbf3a88beeaa7c54e4fad13575f585ce5b26 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Tue, 6 Dec 2016 13:34:06 -0500 Subject: [PATCH] Load icons at sizes given by DPI-dependent system metric - Check system metric for large and small icon sizes and try to load the correct size instaed of scaling from one size. Scaling will still happen if the required size is not available in the icon resource. As we add more icon sizes they will get automatically used as needed. LoadImage scales up from next smallest size available. Revisit this when LoadIconWithScaleDown (Vista+) becomes available in mingw. Resolves Trac: #772 (icon scaling issue) Signed-off-by: Selva Nair --- localization.c | 38 ++++++++++++++++++++++++++++++++++---- localization.h | 1 + openvpn.c | 8 +++++--- tray.c | 4 ++-- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/localization.c b/localization.c index 0220c53..5f05ab8 100644 --- a/localization.c +++ b/localization.c @@ -236,12 +236,22 @@ ShowLocalizedMsg(const UINT stringId, ...) va_end(args); } - -HICON -LoadLocalizedIcon(const UINT iconId) +static HICON +LoadLocalizedIconEx(const UINT iconId, int cxDesired, int cyDesired) { LANGID langId = GetGUILanguage(); + HICON hIcon = + (HICON) LoadImage (o.hInstance, MAKEINTRESOURCE(iconId), + IMAGE_ICON, cxDesired, cyDesired, LR_DEFAULTSIZE|LR_SHARED); + if (hIcon) + return hIcon; + else + PrintDebug (L"Loading icon using LoadImage failed."); + + /* Fallback to CreateIconFromResource which always scales + * from the first image in the resource + */ /* find group icon resource */ HRSRC res = FindResourceLang(RT_GROUP_ICON, MAKEINTRESOURCE(iconId), langId); if (res == NULL) @@ -268,9 +278,29 @@ LoadLocalizedIcon(const UINT iconId) if (resSize == 0) return NULL; - return CreateIconFromResource(resInfo, resSize, TRUE, 0x30000); + /* Note: this uses the first icon in the resource and scales it */ + hIcon = CreateIconFromResourceEx(resInfo, resSize, TRUE, 0x30000, + cxDesired, cyDesired, LR_DEFAULTSIZE|LR_SHARED); + return hIcon; } +HICON +LoadLocalizedIcon(const UINT iconId) +{ + /* get the required normal icon size (e.g., taskbar icon) */ + int cx = GetSystemMetrics(SM_CXICON); + int cy = GetSystemMetrics(SM_CYICON); + return LoadLocalizedIconEx(iconId, cx, cy); +} + +HICON +LoadLocalizedSmallIcon(const UINT iconId) +{ + /* get the required small icon size (e.g., tray icon) */ + int cx = GetSystemMetrics(SM_CXSMICON); + int cy = GetSystemMetrics(SM_CYSMICON); + return LoadLocalizedIconEx(iconId, cx, cy); +} LPCDLGTEMPLATE LocalizedDialogResource(const UINT dialogId) diff --git a/localization.h b/localization.h index b0a84a6..caf067b 100644 --- a/localization.h +++ b/localization.h @@ -28,6 +28,7 @@ int LoadLocalizedStringBuf(PTSTR, const int, const UINT, ...); void ShowLocalizedMsg(const UINT, ...); int ShowLocalizedMsgEx(const UINT, LPCTSTR, const UINT, ...); HICON LoadLocalizedIcon(const UINT); +HICON LoadLocalizedSmallIcon(const UINT); LPCDLGTEMPLATE LocalizedDialogResource(const UINT); INT_PTR LocalizedDialogBoxParam(const UINT, DLGPROC, const LPARAM); HWND CreateLocalizedDialogParam(const UINT, DLGPROC, const LPARAM); diff --git a/openvpn.c b/openvpn.c index 9b5a275..ec636f5 100644 --- a/openvpn.c +++ b/openvpn.c @@ -1712,12 +1712,14 @@ SuspendOpenVPN(int config) void SetStatusWinIcon(HWND hwndDlg, int iconId) { - HICON hIcon = LoadLocalizedIcon(iconId); + HICON hIcon = LoadLocalizedSmallIcon(iconId); if (!hIcon) return; - + HICON hIconBig = LoadLocalizedIcon(ID_ICO_APP); + if (!hIconBig) + hIconBig = hIcon; SendMessage(hwndDlg, WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) hIcon); - SendMessage(hwndDlg, WM_SETICON, (WPARAM) ICON_BIG, (LPARAM) hIcon); + SendMessage(hwndDlg, WM_SETICON, (WPARAM) ICON_BIG, (LPARAM) hIconBig); } diff --git a/tray.c b/tray.c index f4d73e7..ffbf3f3 100644 --- a/tray.c +++ b/tray.c @@ -227,7 +227,7 @@ ShowTrayIcon() ni.hWnd = o.hWnd; ni.uFlags = NIF_MESSAGE | NIF_TIP | NIF_ICON; ni.uCallbackMessage = WM_NOTIFYICONTRAY; - ni.hIcon = LoadLocalizedIcon(ID_ICO_DISCONNECTED); + ni.hIcon = LoadLocalizedSmallIcon(ID_ICO_DISCONNECTED); _tcsncpy(ni.szTip, LoadLocalizedString(IDS_TIP_DEFAULT), _countof(ni.szTip)); Shell_NotifyIcon(NIM_ADD, &ni); @@ -291,7 +291,7 @@ SetTrayIcon(conn_state_t state) ni.cbSize = sizeof(ni); ni.uID = 0; ni.hWnd = o.hWnd; - ni.hIcon = LoadLocalizedIcon(icon_id); + ni.hIcon = LoadLocalizedSmallIcon(icon_id); ni.uFlags = NIF_MESSAGE | NIF_TIP | NIF_ICON; ni.uCallbackMessage = WM_NOTIFYICONTRAY; _tcsncpy(ni.szTip, msg, _countof(ni.szTip));