[quoted text, click to view] Ajak wrote:
> 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
>
>
> "Sergei Gnezdov" <Sergei.G at hotmail.com> wrote in message
> news:48DD326A-BC9E-49AB-8A50-A4FCB98E0173@microsoft.com...
>> 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?
>>
>> "Ajak" <ajakblackgoat@yahoo.com> wrote in message
>> news:O1w3pzcDHHA.996@TK2MSFTNGP02.phx.gbl...
>>> 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
>>>
>
>
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