Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loading specific icon index from an icon resource

Posted on 2007-11-29
11
Medium Priority
?
447 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
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 free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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 1200 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

972 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