简体   繁体   中英

SystemParameters.SmallIconWidth returns 16 on any dpi, so how can I check for actual best icon size?

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM