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.

Oliver Giesen
  • 9,129
  • 6
  • 46
  • 82

1 Answers1

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for the excellent explanation of why it isn't working the way I do it! – Oliver Giesen Nov 26 '10 at 16:28
  • 6
    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 it – David Heffernan Nov 26 '10 at 17:09
  • 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! – Oliver Giesen Nov 29 '10 at 13:05
  • 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. – Premature Optimization Jul 07 '11 at 16:03
  • 1
    @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. – kostix Aug 18 '11 at 15:29