如何向 Windows 询问系统托盘图标的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/568199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:00:50  来源:igfitidea点击:

How do I ask Windows for the size of system tray icons?

windowswinapitaskbarsystem-tray

提问by keelo

I noticed that my app sends icons to the Windows tray with a size of 16x16 pixels--and my Vista PC I've got a doublewide taskbar that appears to show icons at 18x18. The resizing artifacts on my app's icon look awful. How can I ask Windows what size the icons should be?

我注意到我的应用程序将图标发送到大小为 16x16 像素的 Windows 托盘 - 而我的 Vista PC 我有一个双宽任务栏,似乎以 18x18 显示图标。我的应用程序图标上调整大小的工件看起来很糟糕。我如何询问 Windows 图标的大小?

edit:

编辑:

I'm generating the icon dynamically, with a pixel font text overlay. It seems wasteful to generate a bunch of icon sizes dynamically, so it would be nice to avoid building an icon with all the "possible" sizes (not that I'm even sure what those are).

我正在动态生成图标,带有像素字体文本覆盖。动态生成一堆图标尺寸似乎很浪费,因此最好避免构建具有所有“可能”尺寸的图标(我什至不确定它们是什么)。

GetSystemMetrics(SM_CXSMICON) returns 16--the incorrect value.

GetSystemMetrics(SM_CXSMICON) 返回 16——不正确的值。

GetThemeBackgroundContentRect didn't help, either.

GetThemeBackgroundContentRect 也没有帮助。

回答by Mark Ransom

Create your icons in multiple formats, and let Windows select the one it wants.

创建多种格式的图标,让 Windows 选择它想要的一种。

Here's the Wikipedia article on the .ico format.

这是关于 .ico 格式的维基百科文章。

If you really need to know, GetSystemMetricswith a parameter of SM_CXICON or SM_CYICON will tell you the width and height that Windows wants to load.

如果您真的需要知道,带有 SM_CXICON 或 SM_CYICON 参数的GetSystemMetrics会告诉您 Windows 想要加载的宽度和高度。

回答by ReinstateMonica Larry Osterman

Mark's core answer is the right one: Create your icons in multiple formats and let Windows choose the right one. Don't forget to do 32x32 and 64x64 icons for HighDPI scenarios.

Mark 的核心答案是正确的:以多种格式创建图标并让 Windows 选择正确的一种。不要忘记为 HighDPI 场景制作 32x32 和 64x64 图标。

But SM_CXICON/SM_CYICON won't necessarily return the size that will be used in the taskbar. The taskbar chooses the right icon size for it's size (this is muchmore important in Window 7).

但是 SM_CXICON/SM_CYICON 不一定会返回将在任务栏中使用的大小。任务栏选择正确的图标大小为它的大小(这是很多更重要的窗口7)。

Just provide appropriately sized icons and you should be ok.

只需提供适当大小的图标,你应该没问题。

回答by Zooba

Your best bet may be GetThemeBackgroundContentRectpassing TBN_BACKGROUNDas iPartId for the tray notify background.

您最好的选择可能是GetThemeBackgroundContentRectTBN_BACKGROUND作为托盘通知背景的iPartId传递。

GetThemeBackgroundContentRect should return the size defined by the current theme that may be used for drawing without overlapping the borders of the parent element. If I'm reading this correctly, that would be the largest sized notification icon permissible and presumably the size that is being used.

GetThemeBackgroundContentRect 应返回由当前主题定义的大小,该主题可用于绘制而不与父元素的边框重叠。如果我没看错,那将是允许的最大尺寸的通知图标,并且可能是正在使用的尺寸。

Testing with multiple DPI settings is probably the easiest way to tell if this is returning the correct value.

使用多个 DPI 设置进行测试可能是判断这是否返回正确值的最简单方法。