Groups | Blog | Home
all groups > dotnet sdk > november 2006 >

dotnet sdk : How to retrieve application icon


Sergei Gnezdov
11/21/2006 8:13:07 PM
Are you trying to obtain icon from exe file after it was compiled? Is it
enough to have your icon as standalone file in the same directory?

[quoted text, click to view]
Ajak
11/22/2006 12:00:00 AM
thank you for your reply

my application icon is set in the project properties, in order to be used as
the default icon when viewed under windows explorer.
The application contains 3 different windows forms, which I would like to
use the same icon (as the application icon).
If I embed the ico file to my project as embedded resources, and use codes
in the three forms' form_load event to load the icon from resources, my
compiled exe will be increased as it contains two identical icons. Setting
the each form's Icon properties is out of question as that will increase my
exe even more.

In other words, .NET allows us to set the application icon (default icon
showed in windows explorer), and different icon for each forms in the app.
But if we want each forms use the same icon as the application icon, it's a
waste to embed the same icon to the app, resulting in increase exe size. I
couldn't find any form properties to tell it to use the default application
icon.

So, how do I set the forms' icon to load the default application icon?

Thanks


[quoted text, click to view]

jacky kwok
11/22/2006 12:00:00 AM
[quoted text, click to view]

Hello:

I do not sure is there any direct method to get icon in Dotnet.
However, in my job, I use Pinvoke to call API to do so.

The follow code is in C#,

class ClsGetIcon
{
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);

[System.Runtime.InteropServices.DllImport("shell32.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
static extern uint SHGetFileInfo(string pszPath, uint
dwFileAttributes, ref SHFILEINFO sfi, uint cbFileInfo, uint uFlags);
/*
DWORD WINAPI SHGetFileInfo(
LPCTSTR pszPath,
DWORD dwFileAttributes,
SHFILEINFO FAR* psfi,
UINT cbFileInfo,
UINT uFlags
);
*/

//#define SHGFI_ICON 0x000000100 // get icon
//#define SHGFI_SMALLICON 0x000000001 // get small icon
const uint SHGFI_ICON = 0x000000100;
const uint SHGFI_SMALLICON = 0x000000001;

[StructLayout(LayoutKind.Sequential)]
struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
/*
typedef struct _SHFILEINFO {
HICON hIcon;
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO;
*/

static public Icon GetAppIcon(string appPath)
{
SHFILEINFO sfi = new SHFILEINFO();
sfi.hIcon = IntPtr.Zero;
if (0 != SHGetFileInfo(appPath, 0, ref sfi,
(uint)Marshal.SizeOf(sfi), SHGFI_ICON | SHGFI_SMALLICON) &&
sfi.hIcon!=IntPtr.Zero)
{
Icon newIcon = Icon.FromHandle(sfi.hIcon);

Icon returnIcon = (Icon)newIcon.Clone();

DestroyIcon(newIcon.Handle);

return returnIcon;
}
else
return null;
}
}

--
Jacky Kwok
jacky@alumni_DOT_cuhk_DOT_edu_DOT_hk
Ajak
11/22/2006 12:00:00 AM
Hi,

How do I retrieve my application icon (set in the project properties)
through code (using Framework 1.1).
Using Icon constructors is possible but I don't know the icon name to pass
to it.

Thanks

Sergei Gnezdov
11/22/2006 10:51:07 AM
As far as I know pinvoke is the solution if you want to save space. Thanks
for pinvoke code Ajak.

If you don't want to use pinvoke, then you have to have a second copy of the
icon. It either should be a .NET resource or a standalone icon and you will
have to load it manually just like the pinvoke scenario.

[quoted text, click to view]
Ajak
11/23/2006 7:55:53 PM
Thanks guys

I opted to have a second copy of the icon embedded as resource. Make it to
only contain 16x16 res icon.

AddThis Social Bookmark Button