How to load a small system icon?
Asked Answered
B

1

9

I need to display 16x16 pixel icons for error/warning/information. Unfortunately both LoadIcon(0, IDI_*) and LoadImage(0, OIC_*, IMAGE_ICON, 16, 16, LR_SHARED) always give me the 32x32 version of the icon.

I read about ShGetStockIconInfo but that is only available from Vista onwards and I still need to support XP.

Any ideas?

I'm using Delphi 2010 with a TImage component if that matters.

Bendicta answered 26/11, 2010 at 14:1 Comment(2)
Is the icon you're trying to load available as 16x16? If the icon is only 32x32, you always get 32x32.Lassitude
these icons are available in 16/32/48pxStatvolt
S
17

The problem is that when you do it this way you get a cached version of the icon, the first one that the system loaded. That will be the large sized icon, typically 32x32. It matters not what size you specify.

What you can do is find the ID of the desired resource in user32.dll and use something like this:

LoadImage(GetModuleHandle('user32'), MAKEINTRESOURCE(103), IMAGE_ICON,
    16, 16, LR_DEFAULTCOLOR);

You would be better to call GetSystemMetrics(SM_CXSMICON) to get hold of the icon size rather than to hard code 16, but you probably already know that.

I'm not sure where you get the resource IDs from for the resources in user32, or even if they is any guarantee that they will stay constant across different Windows versions. My guess is that they will because too many programs would break, but that's just pure guesswork.

Statvolt answered 26/11, 2010 at 15:40 Comment(5)
Thanks for the excellent explanation of why it isn't working the way I do it!Bendicta
100: IDI_APPLICATION, 101: IDI_WARNING, 102: IDI_QUESTION, 103: IDI_ERROR, 104: IDI_INFORMATION, 105:IDI_WINLOGO, 106: IDI_SHIELD, used ResourceHacker to reverse engineer itStatvolt
So, I can now confirm that this approach works on all the platforms that matter for me (i.e. XP and up, including Server 2003 and 2008). thanks again!Bendicta
Negative. Which caching and how to get rid of it? Also, i do not like the idea of keeping track of module names and resource ids for system-wide resources.Vivavivace
@David Heffernan thanks! I just killed a whole day trying to figure out why (Delphi's) imagelist shows a 32x32 icon when I insert into it an 16x16 icon loaded from user32.dll. The problem is that pervasive on-the-fly resizing of Windows icons made me sure I was really getting 16x16 icon (I even saved it to a file and it was there 16x16 for sure--checked in an image viewer). So you just solved my mistery, thanks.Mouldy

© 2022 - 2024 — McMap. All rights reserved.