Joan Venge
Joan Venge

Reputation: 330872

Where to find the default Winforms icon in Windows?

I assume this is a shared resource somewhere in Windows. Rather than making a copy for each app, is there a way to use this icon just like all Winforms apps use it?

How is this specified for Winforms apps by default? I don't see any reference of any icons in code or project settings. Just that it uses the "default icon".

Upvotes: 6

Views: 25379

Answers (5)

Hans Passant
Hans Passant

Reputation: 941217

It is stored as a resource in the System.Windows.Forms.dll assembly. You could get a copy with Reflector or ILSpy. Open the assembly, open the Resources node, all the way down to "wfc". Right-click, Save, type a name with the .ico extension. Not sure why you'd want to use it, given that it is the default.

You set a custom icon for your application with Project > Properties > Application tab, Icon setting. Each form has its own Icon property that can be set in the designer.

Upvotes: 11

Steven Doggart
Steven Doggart

Reputation: 43743

I had a problem which was similar, but different. Rather than needing to get the default icon, I needed to check to see whether the icon on a form was set or if it was left as the default. While I could have used reflection to get it, I ended up using a simpler solution:

private static Icon defaultIcon = new Form().Icon;

// ...

if(this.Icon == defaultIcon)
{
    // ...
}

Upvotes: 0

SSS
SSS

Reputation: 5393

You can just use the Save method:

C#:

string IcoFilename = "C:\\Junk\\Default.ico";
using (System.IO.FileStream fs = new System.IO.FileStream(IcoFilename, System.IO.FileMode.Create))
{
  this.Icon.Save(fs);
}

Visual Basic:

Dim strFilename As String = "C:\Junk\Default.ico"
Using fs As New System.IO.FileStream(strFilename, IO.FileMode.Create)
  Me.Icon.Save(fs)
End Using

Upvotes: 4

Roberto B
Roberto B

Reputation: 582

It is stored as a resource in the System.Windows.Forms.dll assembly. You could get a copy with reflection as folow:

public static class FormUtils
{
    private static Icon _defaultFormIcon;
    public static Icon DefaultFormIcon
    {
        get
        {
            if (_defaultFormIcon == null)
                _defaultFormIcon = (Icon)typeof(Form).
                    GetProperty("DefaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null, null);

            return _defaultFormIcon;
        }
    }

    public static void SetDefaultIcon()
    {
        var icon = Icon.ExtractAssociatedIcon(EntryAssemblyInfo.ExecutablePath);
        typeof(Form)
            .GetField("defaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
            .SetValue(null, icon);
    }
}

public static class FormExtensions
{
    internal static void GetIconIfDefault(this Form dest, Form source)
    {
        if (dest.Icon == FormUtils.DefaultFormIcon)
            dest.Icon = source.Icon;
    }
}

So as you can see in the code you have in this way the same Icon.Handle. The same reference. Form.DefaultIcon is an internal lazy loaded static property in class Form.

You can also override the default Winforms icon for your application. In Program.cs i use:

FormUtils.SetDefaultIcon();

This function will then override the default icon with the icon specified in your Application properties, the icon of your executable.

Upvotes: 5

Grant Thomas
Grant Thomas

Reputation: 45083

If you have Visual Studio 2010 installed then there is a large collection of icons (potentially including the application icon/s), check out the following directory:

%ProgramFiles%\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\1033

There may be a similar directory for previous VS versions, take a look if needs be.

EDIT:

On doing a search in the folder of the unzipped file for app there are two notable results:

Application.ico and ApplicationGeneric.ico + its *.png counterpart.

If you have VS 2010 and any of the icons in here are suitable, I believe you don't need to copy a single one - you should be able to include the file indirectly (as a shared/linked file) when adding using the Existing Item... dialog; you do this by selecting the arrow next to Add button and selecting the Add As Link option.

What I can't see working as desired is simply overwriting these files in an attempt to apply a global change.

Upvotes: 4

Related Questions