在Windows窗体应用程序中为.exe和窗体使用相同的图标,而不进行复制? [英] using the same icon for the .exe and a form in a Windows Forms application without duplicating it?

查看:76
本文介绍了在Windows窗体应用程序中为.exe和窗体使用相同的图标,而不进行复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个图标,我想将它用作EXE图标和主窗体上的图标.当我说"EXE图标"时,是指/win32icon选项嵌入到C#编译器中的图标,或者是Visual Studio中项目设置的应用程序"部分中指定的图标.这是Windows资源管理器显示的图标.

I have an icon for my application, and I want to use it as both the EXE icon and the icon on the main form. When I say "EXE icon" I mean the icon that is embedded by the /win32icon option to the C# compiler or the icon specified in the Application section of the project settings in Visual Studio. This is the icon displayed by Windows Explorer.

但是,申请表使用默认图标,这是标题栏中以及您按Alt-Tab时显示的图标.

However, the application form uses a default icon, which is what shows up in the title bar and when you press Alt-Tab.

我想在不复制数据的情况下对两者都使用相同的图标.实际上,这意味着WinForms应用程序必须在运行时读取嵌入式Win32图标.大概这是可能的,但是由于搜索结果到处都是有关从.resx文件等访问嵌入式资源的页面,我无法找到任何信息.

I want to use the same icon for both without duplicating the data. Practically, this means the WinForms application has to read the embedded Win32 icon at runtime. Presumably this is possible, but I haven't been able to find any information due to the search results being cluttered with pages about accessing embedded resources from .resx files and the like.

我不介意这是否需要p/invoke或类似要求.我可以通过Win32资源查看器看到该图标已嵌入ID为32512(IDI_APPLICATION)的EXE内.我尝试了以下方法:

I don't mind if this requires p/invoke or similar. I can see with a Win32 resource viewer that the icon is embedded within the EXE with ID 32512 (IDI_APPLICATION). I've tried the following:

IntPtr hInstance = GetModuleHandle(IntPtr.Zero);
IntPtr hIcon = LoadIcon(hInstance, new IntPtr(32512));
icon = Icon.FromHandle(hIcon);

但是hIcon ==0.我也尝试过:

but hIcon == 0. I've also tried:

IntPtr hIcon = LoadIcon(IntPtr.Zero, new IntPtr(32512));
icon = Icon.FromHandle(hIcon);

这会加载一个图标,但这是系统默认的应用程序图标,而不是EXE中的图标.

This loads an icon, but it's a system default application icon, not the one from the EXE.

有人知道怎么做吗?

推荐答案

事实证明,我尝试的第一种方法基本上是正确的.

It turns out that the first approach I tried was basically correct.

IntPtr hInstance = GetModuleHandle(null);
IntPtr hIcon = LoadIcon(hInstance, new IntPtr(32512));
icon = Icon.FromHandle(hIcon);

...函数声明如下:

... where the functions are declared like so:

[DllImport("user32.dll")]
static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr iconName);
[DllImport("kernel32.dll")]
static extern IntPtr GetModuleHandle(string moduleName);

在汉斯·帕桑特(Hans Passant)有点含糊的评论的帮助下,我发现它不起作用的原因是由于Visual Studio托管过程.不想禁用它,我改为将代码有条件地运行:

With help from Hans Passant's somewhat cryptic comment, I found that the reason it didn't work was because of the Visual Studio hosting process. Not wanting to disable that, I instead changed the code to run conditionally:

IntPtr hInstance = GetModuleHandle(null);
IntPtr hIcon = LoadIcon(hInstance, new IntPtr(32512));
if(hIcon != IntPtr.Zero) icon = Icon.FromHandle(hIcon);

然后我们去.不再需要将图标同时复制为Win32和.NET资源数据.

And there we go. No more need to duplicate the icon as both Win32 and .NET resource data.

这篇关于在Windows窗体应用程序中为.exe和窗体使用相同的图标,而不进行复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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