Windows 从多图标文件中选择错误的图标并自行渲染为正确的大小
我有一个 .ico 文件,其中嵌入了 5 个图标大小,用作主应用程序图标和系统托盘图标。
当它显示在任务栏中时,图标使用所需的 16x16 格式。 当图标显示在通知区域/系统托盘中时,它使用 32x32 格式,Windows 将其渲染为 16x16 图标,看起来很糟糕。
如何强制 Windows 在通知区域使用 16x16 图标大小? 这是我将图标放入系统托盘的代码:
ContextMenu cmNotify = new ContextMenu();
MenuItem miNotify = new MenuItem(Properties.Resources.Notify_Text);
miNotify.DefaultItem = true;
miNotify.Click += new EventHandler(notifyHandler);
cmNotify.MenuItems.Add(miNotify);
notifyIcon = new NotifyIcon();
notifyIcon.Icon = this.Icon;
notifyIcon.Visible = true;
notifyIcon.ContextMenu = cmNotify;
notifyIcon.Text = AppConstants.APPLICATION_NAME;
I have an .ico file with 5 icon sizes embedded in it being used as the main application icon and the System Tray icon.
When it shows up in the task bar the icon is using the 16x16 format which is desired.
When the icon shows up in the Notification Area/System tray, it is using the 32x32 format and Windows is rendering it down to a 16x16 icon, which looks horrible.
How do I force windows to use the 16x16 icon size in the notification area?
Here's my code to put the icon in the system tray:
ContextMenu cmNotify = new ContextMenu();
MenuItem miNotify = new MenuItem(Properties.Resources.Notify_Text);
miNotify.DefaultItem = true;
miNotify.Click += new EventHandler(notifyHandler);
cmNotify.MenuItems.Add(miNotify);
notifyIcon = new NotifyIcon();
notifyIcon.Icon = this.Icon;
notifyIcon.Visible = true;
notifyIcon.ContextMenu = cmNotify;
notifyIcon.Text = AppConstants.APPLICATION_NAME;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两种反应都很接近,但都含有微妙的毒药。 您不应将请求的大小硬编码为 16x16。
相反,查询 SystemInformation.SmallIconSize 以确定适当的尺寸。 虽然默认值肯定是 16x16,但这可以通过多种方式更改,例如 DPI 缩放。
请参阅MSDN 文章< /a> 有关此属性的更多信息。
一个用法的例子是
Both responses are close, but contain a subtle poison. You should not hardcode the requested size as 16x16.
Instead, query SystemInformation.SmallIconSize to determine the appropriate dimensions. Although the default is certainly 16x16, this could be changed by various things, such as DPI scaling.
See the MSDN article for more information on this property.
An example of usage would be
将此:更改
为:
Change this:
to this:
您需要创建该图标的新实例。 创建(加载)新实例时,指定大小。 Icon 类构造函数有几种不同的重载供您选择。 如果图标文件嵌入到主可执行文件中(通常是这种情况),则可以执行以下操作:
You need to create a new instance of the icon. When creating (loading) the new instance, specify the size. The Icon class constructor has several different overloads for you to choose from. Here's how you can do it if the icon file is embedded into your main executable (which is often the case):