Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / November 2006

Tip: Looking for answers? Try searching our database.

How to retrieve application icon

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ajak - 22 Nov 2006 00:22 GMT
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 - 22 Nov 2006 04:13 GMT
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?

> Hi,
>
[quoted text clipped - 4 lines]
>
> Thanks
Ajak - 22 Nov 2006 06:01 GMT
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

> 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 clipped - 8 lines]
>>
>> Thanks
jacky kwok - 22 Nov 2006 06:29 GMT
> thank you for your reply
>
[quoted text clipped - 31 lines]
>>>
>>> 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;
    }
}

Signature

Jacky Kwok
jacky@alumni_DOT_cuhk_DOT_edu_DOT_hk
jacky@compose_DOT_com_DOT_hk

Sergei Gnezdov - 22 Nov 2006 18:51 GMT
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.

> Hello:
>
[quoted text clipped - 70 lines]
>     }
> }
Ajak - 23 Nov 2006 11:55 GMT
Thanks guys

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.