0

I thought SystemParameters.SmallIconWidth would tell me what's the best size icon for a small icon. However, even when I change the dpi in Settings->Display to 150% the value of SystemParameters.SmallIconWidth remains the same.

Is there a way to check for the best icon size?

(Why I thought that it should change by dpi - this answer, and this one by a user with more than 500k rep.)

ispiro
  • 26,556
  • 38
  • 136
  • 291

1 Answers1

1

Even if you declare full DPI awareness in the application manifest and/or at run-time, some Windows functions return 96 DPI (or System DPI) values for some reason. You have to pinvoke the new API:

public enum SystemMetric:int { SM_CXSMICON = 49 }
[DllImport("user32.dll")]
static extern int GetSystemMetricsForDpi(SystemMetric smIndex, uint dpi);
[DllImport("user32.dll")]
static extern uint GetDpiForWindow(IntPtr hwnd);

HwndSource source = (HwndSource)HwndSource.FromVisual(this);
IntPtr hWnd = source.Handle;
uint dpi = GetDpiForWindow(hWnd);
int width = GetSystemMetricsForDpi(SM_CXSMICON, dpi);

These functions only exist on Windows 10 update 1607 and later. If you need to support older versions you must manually scale the incorrect DPI value yourself (x = x * dpi / 96) instead of calling these functions.

See also:

Anders
  • 97,548
  • 12
  • 110
  • 164