Solved

Loading specific icon index from an icon resource

Posted on 2007-11-29
11
415 Views
Last Modified: 2013-12-03
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,
0
Comment
Question by:gunn
  • 5
  • 4
11 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 

Author Comment

by:gunn
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
So what is the problem with passing an icon index?
0
 

Author Comment

by:gunn
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
 

Author Comment

by:gunn
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 

Author Comment

by:gunn
Comment Utility
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
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
Comment Utility
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now