1

I'm going through the painful process of making my MFC application DPI aware. I'm planning to include icon resources that contain different icon sizes. For example, small icons for list view controls should get the sizes: 16x16, 20x20, 24x24, etc. as recommend by Microsoft.

I know I can use LoadImage() (or even LoadIconWithScaleDown()) to load an icon of the needed size as follows:

HICON hIcon = (HICON)LoadImage(AfxGetApp()->m_hInstance,
                               MAKEINTRESOURCE(IDI_MYICON),
                               IMAGE_ICON,
                               GetSystemMetrics(SM_CXSMICON),
                               GetSystemMetrics(SM_CYSMICON),
                               LR_DEFAULTCOLOR);

As explained in the Remarks section of the documentation on LoadIconWithScaleDown(), if an icon size is not available, LoadImage() scales a smaller icon up and LoadIconWithScaleDown() scales a larger icon down. However, according to this answer by David Heffernan, I should try to avoid scaling icons (at least small ones):

[...] For instance, if system metrics suggest an 18px icons, and you only have 16px and 20px icons, then make a new 18px icons. Fill the image with transparent pixels, and blit the 16px icon into the middle of this 18px image. Make the icon out of that. [...]

I'd like to do so, but for reasons of robustness, I don't want to make any assumptions on the available icon sizes. Therefore, I want to determine the available icon sizes of an icon resource dynamically at runtime. However, none of the icon functions that I found seem to provide such kind of functionality.

How can I find out, which icon sizes are available in my icon resource (e.g. IDI_MYICON)?

honk
  • 9,137
  • 11
  • 75
  • 83
  • This might help: https://blogs.msdn.microsoft.com/oldnewthing/20120720-00/?p=7083 – Richard Critten Jan 12 '18 at 17:17
  • 1
    There is also [`LookupIconIdFromDirectoryEx`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms648074(v=vs.85).aspx) which could simplify the code. By using this function one could get rid of `GetIconDirectory()` in the linked answer. No need to manually enumerate `GRPICONDIR` structures. – zett42 Jan 12 '18 at 21:51
  • @zett42: If that function always returns the next smaller icon (in case there is no match), then it would indeed help. I wish I would have just 10% of your knowledge ;) Thanks a lot! – honk Jan 12 '18 at 22:04
  • The reference does not explicitly say that it returns the next smaller icon but it mentions that this function is used by `LoadImage()` which does so. – zett42 Jan 12 '18 at 22:45
  • @zett42: This is exactly what I concluded as well :) – honk Jan 12 '18 at 22:48

0 Answers0