Click here to Skip to main content
15,878,814 members
Articles / Programming Languages / C#
Article

Assigning an application’s icon to all forms in the application

Rate me:
Please Sign up or sign in to vote.
4.37/5 (20 votes)
5 Oct 2008CPOL2 min read 100.1K   1.4K   42   15
It is a handy thing if all the windows in an application, by default, have the same icon like the application’s executable. Here is a tip: how to easily assign your application’s icon to all the hosted forms.

Introduction

While developing an application, often, it is a handy thing if all the windows of the application have, by default, the same icon like the application’s executable. However, if you want to do so, you have to manually assign the icon for each form in Visual Studio. If the executable icon is changed, you have to repeat this work again. This article describes a tip for how to assign your application’s icon to all the forms hosted by the application with code.

It is written in C#, and can be used in any .NET language as a DLL.

The most important code for the icon extraction was gotten from here. It was only slightly fixed and enhanced.

Solution

All you have to do is:

  1. Assign an icon to your project in Visual Studio.
  2. Add the classes contained in the folder IconRoutines to your project, or reference IconRoutines.dll in your project.
  3. Add the following line to each form constructor in your app where you want to set the same icon as the application’s icon:
  4. C#
    this.icon = Cliver.IconRoutines.HostIcon;

This will make the windows of your app have the same icon like the app itself. That’s all.

How it works

This section is addressed to inquisitive guys, and can be omitted.

The first step is getting the icon of the executable.

After some time wasted with the .NET Icon class, I understood that correctly extracting the icon from an EXE file (or any file containing icons) is not a simple thing. Why? Because of several hacks there:

  • Icon.ExtractAssociatedIcon extracts only a small image from an icon stored in the file.
  • Icon.Save saves icons in a 16-color format only.

That means the code for extracting icons from files must be written from scratch. Fortunately, such a code was done already by Steve McMahon, and only needed some upgrade. It fetches an icon’s images from an executable. You can find it in the attached sources.

The second step is creating an Icon type object from the fetched images. The following code does it:

C#
/// <summary>
/// Extract all icon images from the library file like .exe, .dll, etc. 
/// </summary>
/// <param name="file">file where icon is extracted from</param>
/// <returns>extracted icon</returns>
public static Icon ExtractIconFromLibrary(string file)
{
    // .NET Icon Explorer (http://www.vbaccelerator.com/home/NET/
    //                     Utilities/Icon_Extractor/article.asp) is used here.
    Icon icon = null;
    try
    {
        vbAccelerator.Components.Win32.GroupIconResources gir = 
              new vbAccelerator.Components.Win32.GroupIconResources(file);
        if (gir.Count < 0)
            return icon;

        vbAccelerator.Components.Win32.IconEx i = 
              new vbAccelerator.Components.Win32.IconEx();
        if (gir[0].IdIsNumeric)
            i.FromLibrary(file, gir[0].Id);
        else
            i.FromLibrary(file, gir[0].Name);

        icon = i.GetIcon();
    }
    catch (Exception e)
    {
        throw (new Exception("Could not extract the host icon.", e));
        icon = null;
    }
    return icon;
}

/// <summary>
/// Icon of the hosting application.
/// </summary>
public static Icon HostIcon
{
    get
    {
        if (_HostIcon == null)
            _HostIcon = IconRoutines.ExtractIconFromLibrary(
                        Application.ExecutablePath);
        return _HostIcon;
    }
}
static Icon _HostIcon = null;

Now, we only have to assign IconRoutines.HostIcon to the form’s icons.

Using the code

In the attached code, you can find:

  • A project IconRoutines that contains the code for the icon management. You can compile it as a DLL and link to your project; else, add the code of IconRoutines to your code.
  • Project Test that references IconRoutines.dll and uses it.

Be happy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect CliverSoft (www.cliversoft.com)
Ukraine Ukraine
Sergey is graduated as applied mathematician. He is specialized in custom development including parsing tools, client/server applications.
github: https://github.com/sergeystoyan
site: http://cliversoft.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 982095827-Jun-13 5:40
Member 982095827-Jun-13 5:40 
GeneralMuch easier way of doing this Pin
MarkyMark788-Dec-10 0:02
MarkyMark788-Dec-10 0:02 
GeneralRe: Much easier way of doing this Pin
ShamanKann10-Sep-11 6:55
ShamanKann10-Sep-11 6:55 
GeneralThanks Pin
dbrenth7-Oct-08 4:46
dbrenth7-Oct-08 4:46 
QuestionWhy not use inheritance? Pin
Rob_III6-Oct-08 22:42
Rob_III6-Oct-08 22:42 
AnswerRe: Why not use inheritance? Pin
Sergiy Stoyan7-Oct-08 7:36
Sergiy Stoyan7-Oct-08 7:36 
GeneralRe: Why not use inheritance? Pin
Rob_III7-Oct-08 9:32
Rob_III7-Oct-08 9:32 
GeneralUse Application Resources Pin
Custec5-Oct-08 22:09
Custec5-Oct-08 22:09 
The easiest way I have found to do this is to add an icon to your project as an embedded resource. You can then just assign the resource to the form icon in the constructor by using the projects properties class:

MyForm.Icon = MyProjectNamespace.Properties.MyIcon.ico;


Requires one line of code per form.

Regards Smile | :)
GeneralRe: Use Application Resources Pin
Sergiy Stoyan5-Oct-08 22:34
Sergiy Stoyan5-Oct-08 22:34 
GeneralRe: Use Application Resources Pin
Custec5-Oct-08 22:38
Custec5-Oct-08 22:38 
GeneralRe: Use Application Resources Pin
net_user12-Mar-09 2:26
net_user12-Mar-09 2:26 
GeneralRe: Use Application Resources Pin
vishalMGiri25-Mar-09 1:06
vishalMGiri25-Mar-09 1:06 
GeneralUsing application icon in forms Pin
Henry Minute5-Oct-08 14:08
Henry Minute5-Oct-08 14:08 
GeneralRe: Using application icon in forms Pin
Sergiy Stoyan5-Oct-08 20:26
Sergiy Stoyan5-Oct-08 20:26 
GeneralRe: Using application icon in forms Pin
Misiu10-Aug-11 8:28
Misiu10-Aug-11 8:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.