winapixojo

Mask missing from HICON on Win10 but not Win7


I am trying to use some system icons such as SIID_DOCNOASSOC and SIID_FOLDER and draw them.

I have the problem that while my code works as expected in Windows 7, on Windows 10 the retrieved images are missing their mask. I cannot figure out why (the PICONINFO.hbmMask field that I can retrieve with GetIconInfo is non-null, indicating that there is a mask, indeed).

My code is written in Xojo, which uses a dialect of VB, but that should hardly matter, as I got it working in Win 7, I'd think:

dim info as SHSTOCKICONINFO
info.cbSize = SHSTOCKICONINFO.Size
SHGetStockIconInfo (SIID_DOCNOASSOC, SHGSI_ICON, info)

dim iconHandle as Integer = info.hIcon
dim destDC as Integer = ... // intialized outside
DrawIconEx (destDC, 0, 0, iconHandle, 0, 0, 0, 0, DI_MASK)

The above code fetched the icon for a plain file and then draws its mask. While the mask is correct on Win 7, the mask is all black over the entire icon's area on Win 10.

Why would that happen?


Solution

  • Windows XP added support for 32-bit ARGB icons with alpha transparency. These icons still contain a black and white mask bitmap but it is often not correct, it depends on the icon editor used and how the artist drew the image! They often look like the my documents icon in this article.

    Vista added support for PNG images in icons (often called "compressed" in icon editors) and contain no mask bitmap. It is not documented what GetIconInfo does to create the mask for these.

    The days of playing with HICON masks are long gone, if you want to draw a icon you should let windows do it for you without extracting the parts of a HICON. ImageList_DrawEx has some blending support if you need it.

    If you absolutely need a mask for some reason then you should build it yourself when the icon contains alpha transparency. Pick some sort of threshold (25, 50, whatever) and treat everything higher than that as transparent when you inspect the alpha values.