🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #203

Re: Loading x48 icons with ExtractIconEx...

#203
From: Remy Lebeau (TeamB)
Date: Thu, 03 Jul 2008 00:31
76 lines
3484 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message 
news:486bef13@newsgroups.borland.com...

> I know how to use LoadLibrary / LoadImage to access a
> give x48 icon. However, I don't know the Icon ID.

If you read the documentation for ExtractIconEx(), it says the index you 
pass to it corresponds to the executable's RT_GROUP_ICON resources.  So you 
would simply extract the first RT_GROUP_ICON resource, see if it has a 48x48 
icon in it, and if so then extract the RT_ICON using the provided icon ID. 
Here is a function for you to do that:

    struct MyEnumData
    {
        UINT nIconIndex;
        HICON hIcon;
    };

    BOOL CALLBACK FindGroupIconProc(HANDLE hModule, LPCTSTR lpszType, LPTSTR 
lpszName, LONG lParam)
    {
        MyEnumData *data = (MyEnumData*) lParam;

        if( data->nIconIndex == 0 )
        {
            HRSRC hRsrc = FindResource(hModule, lpszName, lpszType);
            HGLOBAL hGroup = LoadResource(hModule, hRsrc);
            PBYTE lpGrpIconDir = (PBYTE) LockResource(hGroup);

            int nIconID = LookupIconIdFromDirectoryEx(lpGrpIconDir, TRUE, 
48, 48, 0);
            data.hIcon = LoadImage(hModule, MAKEINTRESOURCE(nIconID), 
IMAGE_ICON, 48, 48, 0);

            return FALSE;
        }

        --(data->nIconIndex);
        return TRUE;
    }

    HICON WINAPI ExtractIcon48(LPCSTR lpszFile, UINT nIconIndex)
    {
        HMODULE hLib = LoadLibraryEx(lpszFile, NULL, 
LOAD_LIBRARY_AS_DATAFILE);
        if( !hLib )
            return NULL;

        MyEnumData data;
        data.nIconIndex = nIconIndex;
        data.hIcon = NULL;

        EnumResourceNames(hLib, RT_GROUP_ICON, 
(ENUMRESNAMEPROC)&FindGroupIconProc, (LONG)&data);

        FreeLibrary(hLib);
        return data.hIcon;
    }

> I want to show the icon that the programs author has
> assigned as the displayed icon.

That would be the icon(s) in the first RT_GROUP_ICON resource.  Even a VCL 
app's "MAINICON" is actually an RT_GROUP_ICON, not an individual RT_ICON 
(though it usually only has 1 icon in the group).

> Using ExtractIconEx will give me the default icon but only
> up to 32x32.

When using ExtractIconEx(), you have to pass an icon index.  It retreives 
the RT_GROUP_ICON at the index, then extracts the contained RT_ICON 
resource(s) for the HICON size(s) you specify.


Gambit 
Technical Details
Message-ID: <486c1e34$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!feed.xsnews.nl!border-2.ams.xsnews.nl!news.isoc.lu!newsgroups.borland.com!not-for-mail
References: <486960ab@newsgroups.borland.com> <486a5a20$1@newsgroups.borland.com> <486b952d@newsgroups.borland.com> <486bb18e$1@newsgroups.borland.com> <486bef13@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,bcbc54027817c35e
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <486960ab@newsgroups.borland.com> <486a5a20$1@newsgroups.borland.com> <486b952d@newsgroups.borland.com> <486bb18e$1@newsgroups.borland.com> <486bef13@newsgroups.borland.com>
Subject: Re: Loading x48 icons with ExtractIconEx...
Date: Wed, 2 Jul 2008 17:31:31 -0700
Lines: 78
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Response
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
NNTP-Posting-Host: 207.178.157.90
Message-ID: <486c1e34$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1215045172 207.178.157.90 (2 Jul 2008 17:32:52 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!feed.xsnews.nl!border-2.ams.xsnews.nl!news.isoc.lu!newsgroups.borland.com!not-for-mail