I use the following code to get mouse cursor bitmap:
HCURSOR hCursor = (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
ICONINFO info = { 0 };
BOOL ret = GetIconInfo(hCursor, &info);
When I save the info.hbmMask
bitmap to a file, it looks like this:
I want to use this cursor as a Direct3d9 texture for drawing. I do not know how to convert this monochrome bitmap into an RGB byte buffer which can be used for creating the texture.
Or is there any other way to get an RGB byte array of a standard Windows cursor?
An icon consists of two bitmaps working together, as described in the ICONINFO
documentation:
hbmMask
Type: HBITMAPThe icon bitmask bitmap. If this structure defines a black and white icon, this bitmask is formatted so that the upper half is the icon AND bitmask and the lower half is the icon XOR bitmask. Under this condition, the height should be an even multiple of two. If this structure defines a color icon, this mask only defines the AND bitmask of the icon.
hbmColor
Type: HBITMAPA handle to the icon color bitmap. This member can be optional if this structure defines a black and white icon. The AND bitmask of hbmMask is applied with the SRCAND flag to the destination; subsequently, the color bitmap is applied (using XOR) to the destination by using the SRCINVERT flag.
In your case, you have a monochrome icon, so hbmColor
is NULL and hbmMask
contains both the mask and the colors. The top half is AND
'ed with the target to clear pixels and create an empty space for the icon, and then the bottom half is XOR
ed with the target to fill in the space created by the mask.
For a non-monochrome icon, hbmMask
would be AND
'ed as-is with the target, and then hbmColor
would be XOR
ed as-is with the target.
As Raymond Chen stated in his comment, you can "use GetDIBits()
to extract the bits from a bitmap". So you have to extract the pixel bits from the appropriate HBITMAP
and process them according to whether you are working with a monochrome icon or not.