NET避免重复图标资源

NET避免重复图标资源

本文介绍了在.NET避免重复图标资源(C#)项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual C#2008 Express的。我想使用相同的图标为应用程序(即,显示了该.exe的图标),并为主要形式。不幸的是,VC#似乎并没有被这个非常聪明,并坚持复制图标数据。

I'm using Visual C# 2008 Express. I'd like to use the same icon for the application (ie, the icon shown for the .exe), and for the main form. Unfortunately, VC# doesn't seem to be very smart about this, and insists on duplicating the icon data.

似乎没有成为一种选择的一种方式在窗体或项目图标(只选择文件),并使用这两个图标相同的文件用已嵌入图标只嵌入文件两次,据我所看到的。这不是什么大不了的事(硬盘空间是便宜了,对吧?),但它的错误我。

There doesn't seem to be a way of selecting an "already embedded" icon for use in the form or project icon (only selecting a file), and using the same file for both icons just embeds the file twice as far as I can see. It's not a big deal (hard drive space is cheap now, right?), but it bugs me.

任何想法如何避免这种情况?有没有办法在构建窗体时加载编程可执行文件的图标用,说什么?一对夫妇对类似的事情论坛帖子似乎表明,.NET资源不正常使用旧的Windows资源系统 - 有从旧式的资源获取的框架内的一种方式?或者,我必须绑定Win32 API函数做呢?

Any idea how to avoid this? Is there a way to programatically load the executable's icon for use when the form is constructed, say? A couple of forum posts about similar things seem to suggest that .NET resources don't use the normal old Windows resource system -- is there a way from within the framework of getting at the old-style resources? Or do I have to bind the Win32 API functions to do it?

推荐答案

是啊,这是相当烦人。但随着Icon.ExtractAssociatedIcon的建议回答的问题是,它将检索32x32的图标,然后下采样,以在表单窗口或在任务栏上一个16x16的图标,这将看起来很糟糕,除非你的32x32的图标非常巧妙地构造。

Yeah, it's pretty annoying. But the problem with the proposed answer of Icon.ExtractAssociatedIcon is that it will retrieve the 32x32 icon, and then downsample to a 16x16 icon in your forms window or on the taskbar, which will look terrible unless your 32x32 icon is very cleverly constructed.

我在做它的方式是互操作(把你的表单构造的第一行):

The way I'm doing it is with interop (put the first line in your form constructor):

this.Icon = ExtractSmallIconFromLibrary(Application.ExecutablePath);
...

public static Icon ExtractSmallIconFromLibrary(string file) {
    IntPtr[] reficon = new IntPtr[1];
    int nextracted = ExtractIconEx(file, 0, null, reficon, 1);
    if (nextracted < 1)
        return null;
    Icon unmanaged_icon = Icon.FromHandle(reficon[0]);
    Icon icon = (Icon)unmanaged_icon.Clone();
    DestroyIcon(unmanaged_icon.Handle);
    return icon;
}

[DllImport("Shell32", CharSet = CharSet.Auto)]
extern static int ExtractIconEx(
    [MarshalAs(UnmanagedType.LPTStr)]
    string lpszFile,
    int nIconIndex,
    IntPtr[] phIconLarge,
    IntPtr[] phIconSmall,
    int nIcons
    );

[DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);



但是,这是不是很大,或者说,因为你的的希望32x32的图标之类的使用Alt-Tab图标列表。所以,你真的需要提取的全部的图标,这是一个更大的工作。也许有两个图标组合成一个简单方法。或者你可以像这样计划,其中有一大堆提取摆在首位,整个图标代码。

But this isn't great, either, since you do want the 32x32 icon for things like the Alt-Tab icon list. So you really need to extract the entire icon, which is a bigger job. Maybe there's a straightforward way to combine the two icons into one. Or you can do like this codeproject program, which extracts the whole icon in the first place with a huge pile of code.

这篇关于在.NET避免重复图标资源(C#)项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:28