Link to home
Start Free TrialLog in
Avatar of gunn
gunn

asked on

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,
Avatar of jkr
jkr
Flag of Germany image

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")
Avatar of gunn
gunn

ASKER

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,
So what is the problem with passing an icon index?
Avatar of gunn

ASKER

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,
>>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.
Avatar of gunn

ASKER

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... !!

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
}
Avatar of gunn

ASKER

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,
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial