Loading specific icon index from an icon resource

Currently, I am using LoadImage() to get a handle to an icon that is a resource in my Win32 Application project (using VS2005). Now though, I've added more icons to this icon resource...I now have 3 icons within it, each of 16x16 size, but of different colors (a normal, highlighted and disabled icon).

It looks like LoadImage() is the preferred function to use, but I can't figure out how to extract an icon with a specific index from my icon resource.

Functions like ExtractIcon(), etc. need to be supplied with a path to an .exe, or .ico. I just have it as a resource in my app (ie: IDM_RESET).

Any ideas on the best way to get the icon I want?

Thanks,
gunnAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkrConnect With a Mentor Commented:
Ah, now I see what you mean. In that case see the part 'DLL and EXE Files' in http://msdn2.microsoft.com/en-us/library/ms997538.aspx ("Icons in Win32") on how to do that using a GRPICONDIRENTRY's nID member that can be passed to FindResource, LoadResource and LockResource to obtain a pointer to the ICONIMAGE structure.
0
 
jkrCommented:
Take a look at http://www.codeproject.com/cpp/GetIconsfromExeorDLLs.asp ("Get icons from Exe or DLL the PE way") and http://www.codeproject.com/win32/icon_viewer.asp ("An Icon Explorer")
0
 
gunnAuthor Commented:
Man, I've looked over the articles you mentioned, and a bunch of other articles, but none are doing what I want. Most are using ExtractIcon(), and the like which pass in a path to the exe or ico file. In those functions, you pass an index to get the one you want.

But I have the icon as a resource. One suggestion I read was to add the icons that I want with very slightly different sizes (say 16x16, 16x17, 16x18) so that I can use LoadImage() to get the one I want, but then always draw them as 16x16. Might have to go that way, but I'd rather not have to redo all my ICO files.

I really didn't think this would be as tough as this!
Thanks,
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jkrCommented:
So what is the problem with passing an icon index?
0
 
gunnAuthor Commented:
There doesn't seem to be a way to do just that! In LoadImage(), there is no option to pass an icon index.
In ExtractIcon(), you can pass an index, but you must also pass the name of the .exe, .dll or .ico it is found in. Well, I have it as a resource in my application. I don't want to carry around the baggage of included the .ico's as part of the installation.

So, I need a way to get a specific icon index from an icon included as resource in the application. A combination of the above...

Thanks,
0
 
jkrCommented:
>>So, I need a way to get a specific icon index from an icon included as resource
>>in the application.

Err, since you specify that index in the .rc file, that should not be rocket science. You are assigning the index, thus you should know it.
0
 
gunnAuthor Commented:
For example, In my resource file, I have an icon resource IDM_RESET. It is an icon resource, with three images embedded within it.

I can get to the first icon within it by simply using the LoadImage() function, passing IDM_RESET and IMAGE_ICON as parameters to that function. This will return an HICON. Cool.

But, how can I get the second icon, or third icon within this resource? I have no clue... !!

0
 
jkrCommented:
Well, gather your icon IDs in an array, e.g.

UINT icons[] = { IDM_RESET, IDM_SOMETHINGELSE, IDM_WHATEVER, 0};

for (UINT i = 0; icons[i] != 0; ++i) {

  // use icon[i] to access the i-th icon
}
0
 
gunnAuthor Commented:
Thanks for the reply. I don't see how that code snippet gives me anything more than I would already know.
In code, I can already get a handle to the resources by getting them directly, ie: LoadImage(hInstance, IDM_RESET, ...)
But *within* that one single icon resource, I have three images embedded, that are all 16x16 size. How to get the 2nd and 3rd images as HICON's??
Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.