在运行时更改任务栏中显示的图标 [英] Change icon displayed in taskbar at runtime

查看:390
本文介绍了在运行时更改任务栏中显示的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows 7应用程序,我需要在运行时更改主窗体上显示的图标和Windows在任务栏上显示的图标。



在代码中我只是更改了Form.Icon属性,如果我从EXE运行应用程序,这没有问题。表单和任务栏上的图标更改没有问题。



但是,如果我从桌面快捷方式运行应用程序,图标只会在主窗体中更改,并且任务栏上显示的图标永远不会更改。显然,Windows正在从缓存中加载任务栏图标。我可以找到关于如何刷新缓存任务栏图标的唯一建议是删除iconcache然后重新启动Windows。这个解决方案显然无法在运行时更改图标。



有没有人知道如何通过桌面快捷方式在运行时更改任务栏图标? ?我猜有一些api函数可以工作,但我无法弄清楚。

I have a Windows 7 application where I need to change the icon displayed on the main form and the icon that is displayed by Windows on the taskbar at runtime.

In code I am simply changing the Form.Icon property and this works no problem if I run the app from the EXE. The icon changes in the form and on the taskbar no problem.

However, if I run the app from a desktop shortcut the icon only changes in the main form and the icon shown on the taskbar never changes. Apparently Windows is loading the taskbar icon from a cache. The only suggestions I can find on how to refresh the cached taskbar icon are to delete the iconcache and then restart windows. This solution obviously will not work for changing the icon at runtime.

Does any one know how change the taskbar icon at runtime for an app launched via a desktop shortcut??? I am guessing there are some api functions that will work, but I cannot figure it out.

推荐答案

我想到了一个非常简单的解决方案,可能适用于其他人。我的根本问题是由于Windows如何缓存应用程序快捷方式的图标。这阻止我在运行时更改任务栏图标。这肯定是可能的。因此,我没有让开始快捷方式直接指向主应用程序exe,而是指向启动器应用程序的快捷方式,该应用程序将弹出到主应用程序。这样,主应用程序exe永远不会从快捷方式启动。因此,可以在运行时更改任务栏上的图标。这对我来说非常好。
I thought of a very easy work-around that might work for others. My underlying problem was due to how Windows caches the icons of application shortcuts. This was preventing me from changing the taskbar icon at runtime. Which is certainly possible. So instead of having the start shortcut pointing directly at the main application exe, I am pointing the shortcut to a launcher application that shells out to the main application. This way, the main application exe is never launched from a shortcut. Thus, the icon on the taskbar can be changed at runtime. This is working very well for me.


请参阅我对该问题的评论。为什么?



现在,只是一些笔记。表单图标是另一回事,您可以在运行时轻松更改它。它甚至可能有一定意义(不同的操作模式,不同的功能部分)。任务栏中显示的图标是另一回事,它被称为应用程序图标。当操作系统使用它时,它应该以某种通用方式存储在PE模块(文件)中,而不是特定于.NET。应用程序图标实际上是在应用程序清单中构建的。对于程序集,它存储在模块中,该模块包含程序集的应用程序清单。 (Visual Studio仅支持每个程序集创建一个模块,但在较低级别编译将允许每个程序集有多个,但只有一个将保存清单;它被称为主程序集模块 。)



MSBuild使用此构建步骤中的图标文件: http://msdn.microsoft.com/en-us/library/microsoft.build.tasks.generateapplicationmanifest.aspx [< a href =http://msdn.microsoft.com/en-us/library/microsoft.build.tasks.generateapplicationmanifest.aspxtarget =_ blanktitle =New Window> ^ ]。



也就是说,与表格图标不同,此图标只读。而且,正如您应该知道的那样,无论您的权限有多高,都无法修改加载执行的可执行模块。这是大多数现代操作系统的重要保护功能。



即使您可以通过某种方式更改此图标(不,我不认为它''可能),它会打败他们的目的。操作系统控件依赖于此图标,以确保您的应用程序易于识别;如果您可以更改它,它将不会收到任何通知,因为这不是设计的方式。



-SA
Please see my comment to the question. Why?

Now, just some notes. The form icon is a different thing, you can really change it easily during runtime. It may even make some sense (different mode of operation, different part of functionality). The icon shown in the Taskbar is a different thing, it is called application icon. As OS uses it, it should be stored in the PE module (file) in some universal way, not specific to .NET. The application icon is actually build in application manifest. For an assembly, this is stored in a module, the one holding an application manifest for the assembly. (Visual Studio supports only creation of the assembles with one module per assembly, but compiling on lower level will allow you to have more then one per assembly, but only one of them will hold the manifest; it''s called "main assembly module".)

The MSBuild uses the icon file in this build step: http://msdn.microsoft.com/en-us/library/microsoft.build.tasks.generateapplicationmanifest.aspx[^].

That said, unlike the form icon, this icon is readonly. And, as you should know, the executable module loaded for execution cannot be modified, no matter how high your permissions are. This is an important protection feature of most modern OS.

And even if you could change this icon by some means (no, I don''t think it''s possible), it would defeat the purpose of them. OS controls rely on this icon to keep your application well-recognizable; it would not get any notifications if you could change it, because this is not how things are designed.

—SA


这篇关于在运行时更改任务栏中显示的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆