Solved

Loading specific icon index from an icon resource

Posted on 2007-11-29
11
441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20376206
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
ID: 20395924
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
ID: 20396725
So what is the problem with passing an icon index?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:gunn
ID: 20397650
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
 
LVL 86

Expert Comment

by:jkr
ID: 20398493
>>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
ID: 20398647
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
ID: 20399375
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
ID: 20402712
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
ID: 20466774
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

630 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