Thread: Loading icons from other programs

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Loading icons from other programs

    I'm trying to make a file view which uses icons taken from the registry. In HKEY_CLASSES_ROOT most (all?) types have a DefaultIcon key on the format "XXX.dll, 2" or similar. My guess is that the first part is the exe/dll the icon lies in and the second is some kind of resource id.

    Anyway, most of the times I'm unable to load the icon using the code below (sometimes it works but it's very rare). LoadLibrary succeeds but LoadIcon returns NULL. What am I doing wrong/missing?
    Code:
    HINSTANCE L = LoadLibrary("C:\\Program\\WinZip\\winzip32.exe");
    HICON I = LoadIcon(L, MAKEINTRESOURCE(0));
    (the DefaultIcon for zip-files are "C:\Program\WinZip\winzip32.exe, 0")

    Footnote: Changing 0 to 1 makes it load a zip-icon...

    EDIT: The error code is 1813 - "The specified resource type cannot be found in the image file." btw...
    Last edited by Magos; 02-27-2006 at 07:25 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Just a guess: ExtractIcon

    I'm actually interested in knowing how to do this for a project I'm working on. So you can find different file extensions in the registry, and it has a dll or exe file listed?

    Edit: Or maybe this would work even better
    Last edited by JaWiB; 02-27-2006 at 07:21 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    ExtractIcon seems to work just fine, way less code too.

    You're linking to the same LoadIcon page all the time btw
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Oh what the...My second link was 10x better. Try ExtractAssociatedIcon instead.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yes, it saves some regedit trouble but it seems it cannot retrieve the icon from just an extention, it needs a physical existing file which is not (always) the case for me.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well for me that works a lot better since the file will always exist
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    To retrieve an icon for an extension or file, the recommended function is SHGetFileInfo with SHGFI_ICON. This function has options to control different properties such as icon size, highlight, and whether the shortcut overlay is added.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Ok, all these methods seems to be able to load the icon fine but I'm having troubles adding the icons to a HIMAGELIST structure. I'm pretty sure it's a mismatch in the icon formats somewhere but how can you 'transform' the icon to a small icon with the proper color format?

    The ImageList_AddIcon fails every time:
    Code:
    IconList = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR8 | ILC_MASK, 0, IconContainer.GetSize());
    
    Size = IconContainer.GetSize();
    for(i = 0; i < Size; i++)
    {
       Index = ImageList_AddIcon(IconList, IconContainer[i]);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I've gotten it to work better now. However SHGetFileInfo doesn't seem to recognize html extentions and some of the icons have black artifacts in them (see screenie). Ideas?

    http://img45.imageshack.us/img45/111...creenie0wr.png
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Those are likely to be the winxp kiddie style icons (ones with drop shadow etc.) - they should probably render properly if your app uses a manifest, either as a compiled resource or external file.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I don't understand what you mean, what's a manifest?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Using winxp visual styles. Search this board, too, as I believe you may find some explicit examples.

    Without a manifest, xp style icons look horrible(like in your image) but should render properly with the manifest.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cargo loading system
    By kyle rull in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 12:16 PM
  2. Loading custom icons
    By Cilius in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2005, 05:38 PM
  3. loading in other programs
    By phantom in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2002, 11:25 AM
  4. icon's fro programs
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 04-01-2002, 05:54 PM
  5. Adding Icons to your programs
    By Dual-Catfish in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2002, 04:50 PM