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 <selva.nair@gmail.com>
pull/107/head
Selva Nair 2016-12-06 13:34:06 -05:00
parent 270ea5f160
commit 2f2ddbf3a8
4 changed files with 42 additions and 9 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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);
}

4
tray.c
View File

@ -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));