Posts Tagged ‘ win32 ’

Creating an alpha-blended cursor in Win32

MSDN has a fairly nice article about how to create an alpha-blended mouse cursor in Win32.

You can read the article at

The long and short of it is that you create a BITMAPV5HEADER structure to describe your bitmap, then you call CreateDIBSection to create a DIB to hold it, then copy your pixels in, and bam, it works.

Well, most of the time.

I’ve spent multiple hours today wrestling with this code which wasn’t working for me. I couldn’t get the alpha channel to work properly. At first I didn’t think the code worked at all, but with some input from others, I realized that the code did work exactly as it was listed, but that I had modified it a little.

Specifically, I was trying to alter the byte ordering. The BITMAPV5HEADER struct has 4 parameters, bV5RedMask, bV5GreenMask, bV5BlueMask, and bV5AlphaMask, which ostensibly allow you to configure which bits in the bitmap correspond to which colors.

The example, as given, was arranging the bits as AARRGGBB, which I believe would typically be called BGRA and I believe is typically the default on Windows due to its little-endian nature. However, the bitmap I wanted to use as a mouse cursor was arranged AABBGGRR (RGBA) as it had its origins in OpenGL land.

After wrestling with this for hours, I came to a conclusion. You just can’t use an RGBA bitmap to create an alpha-blended mouse cursor. I haven’t gone to the lengths of testing if you can create an RGBA HBITMAP at all (my guess: no), but it definitely does not work for mouse cursors. No way, no how, no amount of beating it with a stick. It won’t work.

I had to end up coercing the bitmap to BGRA (which isn’t hard, or all that expensive, but irritating), and now it works fine.

My guess is that there is some sort of optimization going on deep under the hood, but unless Microsoft wants to speak up about it, I doubt I’ll ever know. I just wanted to get this information out ‘on the webs’ because Google was sure failing me all day when trying to research the problem.