cool hit counter [C#] Note the use of icons in a "cost-effective" way_Intefrankly

[C#] Note the use of icons in a "cost-effective" way

First, let me explain what is meant by "cost-effective". Suppose a Winform program contains several forms, each of which is to display an icon in the upper left corner (i.e., the Form.Icon property is to be set), and the program itself is to have an icon (for display in OS Explorer), all of which look the same - a very common scenario. As pictured.

That is, the same icon is to be used on top of the program itself and the individual forms in the program. So by "cost effective" I mean that only one copy of the icon data is stored in the program file (exe) and all the places where the icon is used are taken from there. Instead of storing multiple copies and taking each one, it is obvious that this will increase the size of the program and is not "cost effective".

The reason for this topic is that if you don't pay attention to the mechanics, you can end up with multiple copies of the same icon stored, and VS and the compiler aren't smart enough to automatically remove redundant resources for us. So the key to being cost-effective and not wasteful is to figure out what kind of results each operation will cause.

First, see how to set the program icon

There are several options for this place.

1、Browse directly to the ico file for selection. VS will automatically copy the ico file to the project root

2、Store the ico file to the project root directory or any subdirectory (the directory must be "included in the project"), and then you can drop down and select it here. This is the case for Resources est.ico and test.ico as shown above

Regardless of the selection method, after the project is compiled into a PE file, this icon is stored in the resource section of the PE file, which can be seen with a tool like eXeScope. For information on PE, please see.

In other words, there's nothing to notice about the selection of program icons, because the results are the same.

Second, the method of setting the form icon

1. Browse the ico file directly in the properties panel of VS. As pictured.

This is probably the most intuitive and simple way to do it. But unfortunately, this happens to be the most wasteful method, because the icons selected in this way are embedded in the resources of the corresponding form (Form.resx), and with several forms setting icons in this way, several copies of the icon data will be stored.

2. Add the icon to the project resources (Resources.resx). Then set it up like this in all the form code.

This. Icon = xx.Properties.Resources.test;//xx is the project's default namespace; test is the resource name

The result of this approach is that 1 copy of the icon is stored in the assembly as an assembly resource, and all forms share this resource. Compared to the 1st way, this way does not result in multiple copies of the icon being stored. But it also only solves the problem of multiple forms sharing a single icon, and the problem of the icon for the program itself.

As mentioned above, there is only one place to set the program icon, and the result of setting it is to store the icon into PE resource. Here there are two concepts of [program set resource] and [PE resource], that is, although there is only one copy of icon data in the program set resource, as soon as the program icon is set, the project will store another copy of the icon into PE resource when compiling, so there are still two copies of icon data in the whole PE file. Then to have the program and the form share a resource, there are two ideas, one is to have the program icon use the assembly resource, and the other is to have the form use the PE resource.

For the former, how can I go looking for abuse, even if the self-government may not succeed in the matter, PASS~

3. Get the program icon and give it to the form to use. The natural thing that came to mind at first was Icon.ExtractAssociatedIcon(), but this method only fetches 32x32 icons, and the form icons are 16x16, which causes scaling that is unacceptable for someone like me who obsesses over visual details. Ideally, the complete icon set (a collection of icons with multiple sizes and color depths) is fetched and assigned to the form Icon so that it has perfect representation in the upper left corner of the form and the NT6 taskbar. As pictured.

So how do you fetch the icon set. For this reason I nibbled on a number of coders who started out by .net for the knowledge that I could not do, including SHGetFileInfo, LoadIcon, LoadImage, ExtractIcon, FindResource and other APIs, and even nibbled on the PE structure, OMG ~ the more I nibbled, the more I felt like dying. God is fortunate to have found a ready-made solution from a high achiever at

Thanks in advance to this kind man, good people live in peace. He is quite a full program, you can get icons from a variety of sources, originally wanted to streamline it, as long as the method to get the PE icon set, but found that the whole program, most of the code is to do this, streamlining does not make much sense, so simply use the whole thing. Back on topic, in all forms write.

this.Icon = IconHelper.ExtractIcon(Application.ExecutablePath, 0);

That is all. Of course I modified his IconHelper slightly by adding the AppIcon property at

static Icon appIcon;
/// <summary>
///  Main program icon
/// </summary>
public static Icon AppIcon
        if (appIcon == null)
            try { appIcon = ExtractIcon(Application.ExecutablePath, 0); }
            catch { return null; }
        return appIcon;

Thus, to the form, write as. this.Icon = IconHelper.AppIcon;

That is all.

At this point, the purpose of sharing one icon between the program and the form and storing only 1 copy of the icon data in the program PE file has been achieved. Thanks again, high and mighty! It's just that the project will probably add close to 20K to the size of the final generated program after adding that solution. So whether or not it is cost effective to use that option needs to be weighed in practical terms. For example, if the icon file is not large, and the two icons do not add up to 1 icon + 20K, then save two copies, the ultimate goal is the size of the program anyway, and not to pursue complete sharing.

---------------------------------- why do you have to name the dividers ----------------------------------

Also, by the way, for tray area icons, if you set an icon group for NotifyIcon.Icon, it doesn't automatically take the 16x16 one, it takes the 32x32 one, like this.

Had to just throw a 16x16 at it to make it work, so if you already have the icon set, gotta set the tray icons like this:.

notifyIcon1.Icon = new System.Drawing.Icon(IconHelper.AppIcon,16,16);// means a single icon of the specified size from the icon set.

This will give you the perfect tray icon:.

---------------------------------- another divider ----------------------------------

Finally, a short illustrative diagram of the various resources in the project is attached.

Lastly ~ when did I get so long-winded? The "cost effective" aspect of the article is only at the file system level, which is to uniquely store the icon data in the PE file as much as possible to reduce the size of the program. However, at the memory level, the above method may cause multiple copies of the icon data, i.e. the file is smaller, but it may not take up much memory to run, I didn't prove this, I'll pursue the memory level "cost effective" when I have egg on my face.

Text Bi.

1、Python using turtle to draw yin and yang fish patterns
2、On the surface Wang Haoxin is introduced as a KOL but he is a deep web hacker in the dark world
3、Shenzhen officially announced Goodbye invoice
4、Shaoxing just finished the letter olympiad question analysis
5、Almost became a coder

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送