SystemParameters.SmallIconWidth 在任何 dpi 上都返回 16,那么如何检查实际的最佳图标大小? [英] SystemParameters.SmallIconWidth returns 16 on any dpi, so how can I check for actual best icon size?
问题描述
我认为 SystemParameters.SmallIconWidth会告诉我什么是小图标的最佳尺寸图标.但是,即使我将 Settings->Display 中的 dpi 更改为 150%,SystemParameters.SmallIconWidth
的值也保持不变.
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?
(为什么我认为它应该通过 dpi 改变 - 这个答案,以及 这个 来自拥有超过 50 万代表的用户.)
(Why I thought that it should change by dpi - this answer, and this one by a user with more than 500k rep.)
推荐答案
即使您在应用程序清单中和/或在运行时声明完全 DPI 感知,某些 Windows 函数也会为某些函数返回 96 DPI(或系统 DPI)值原因.您必须调用新 API:
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);
这些功能仅存在于 Windows 10 更新 1607 及更高版本中.如果您需要支持旧版本,您必须自己手动缩放不正确的 DPI 值 (x = x * dpi/96
) 而不是调用这些函数.
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.
另见:
这篇关于SystemParameters.SmallIconWidth 在任何 dpi 上都返回 16,那么如何检查实际的最佳图标大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!