从代码更改WPF程序集图标 [英] Change WPF Assembly Icon from Code

查看:120
本文介绍了从代码更改WPF程序集图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从代码中更改WPF程序集图标?我不是指窗口图标,而是指.exe文件中显示的图标。

Is there a way to change a WPF assembly icon from code? I'm not referring to the window icon, but to the icon that appears on the .exe file.

编辑:

我正在尝试在应用程序图标的表示中实现交互性 - 不同的用户启动的操作与当前状态相结合应该会导致不同的应用程序图标。我依赖于应用程序的可视化表示,因为它没有可见窗口,并且交互基于热键和一般系统使用模式。

I'm trying to achieve interactivity in the application icon's representation - different user-initiated actions combined with a current state should lead to a different application icon. I rely on the visual representation of the application as it has no visible window and the interaction is based on hot-keys and general system usage patterns.

推荐答案

概述

更改.exe文件中的图标虽然有点麻烦但很简单。您可能需要做三件事:

Changing the icon in your .exe file is straightforward though a bit cumbersome. You'll potentially need to do three things:


  1. 阻止您的运行进程锁定.exe文件,这会阻止它正在修改

  2. 可能修改文件权限以使其可写

  3. 实际编辑.exe文件以替换图标

详细信息

第3步 - 实际编辑.exe文件 - 是最有趣的,所以我会从那里开始。您将使用 BeginUpdateResource() UpdateResource() EndUpdateResource() kernel32.dll 中调用。基本上你这样做:

Step 3 - actually editing the .exe file - is the most interesting, so I'll start there. You will use the BeginUpdateResource(), UpdateResource() and EndUpdateResource() calls in kernel32.dll. Basically you do this:

byte[] data = File.ReadAllBytes(newIconFilePath);
       // Or otherwise load icon data

IntPtr hUpdate = BeginUpdateResource(exePath, false);
UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(1), LANG_SYSTEM_DEFAULT,
               data, data.Length);
EndUpdateResource(hUpdate, false);

您需要添加 DllImport 声明为函数和实现常量。有关如何使用的详细信息,请参阅 MSDN上的文档 BeginUpdateResource UpdateResource EndUpdateResource work。

You'll need to add DllImport declarations for the functions and implement the constants. See the documentation on MSDN for details on how BeginUpdateResource, UpdateResource, and EndUpdateResource work.

对于第1步 - 防止.exe被锁定 - 简单的解决方案是向应用程序启动添加代码,检查当前.exe是否从临时目录运行( Path.GetTempPath())。如果没有,它会使用 File.Copy()将.exe复制到临时目录以及所需的任何其他文件,然后使用一个额外的命令行参数执行它。原始.exe的位置。然后退出原始进程,删除.exe文件上的锁。

For step 1 - preventing your .exe from being locked - the easy solution is to add code to your application startup that checks to see if the current .exe is running from the temporary directory (Path.GetTempPath()). If not, it copies the .exe to the temporary directory using File.Copy() along with any additional files needed, then executes it with one additional command line argument that gives the location of the original .exe. The original process then exits, removing the lock on the .exe file.

对于步骤2 - 更正权限 - 这只需修改ACL并可能触发UAC对话。有很多例子,你可能不需要这样做,所以我会跳过进一步的解释

For step 2 - correcting permissions - this simply a matter of modifying the ACLs and possibly triggering a UAC dialog. There are plenty of examples out there and you probably don't need to do this, so I'll skip further explanation

最后的注释

上述步骤实际上允许您编辑真实.exe文件的实际图标。但是,如果您只需要更改可视图标,我建议您使用快捷方式并改为编辑其图标。

The above steps will actually allow you to edit the actual icon of your real .exe file. However if you just need a visual icon change I would recommend you use a shortcut and edit its icon instead.

这篇关于从代码更改WPF程序集图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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