Windows cursor transparency

Started by
2 comments, last by Juliean 10 years, 1 month ago

Hello,

I'm trying to load a mouse cursor for windows from an image-file. How do I calculate the correct and + xor-masks for transparent images? I'm using the method from CodeProject.com, which works fine for simple cursors without an alpha-channel:


gfx::TextureAccessorX32 accessor(texture, true);
const gfx::Color3 transparent(254, 254, 254);
for(unsigned int x = 0; x < (unsigned int)vSize.x; x++)
{
    for(unsigned int y = 0; y < (unsigned int)vSize.y; y++)
    {
        auto& pixel = accessor.GetPixel(x, y);

        if(pixel == transparent)
        {
            SetPixel(hAndMaskDC, x, y, RGB(255, 255, 255));
            SetPixel(hXorMaskDC, x, y, RGB(0, 0, 0));
        }
        else
        {
            SetPixel(hAndMaskDC, x, y, RGB(0, 0, 0));
            SetPixel(hXorMaskDC, x, y, RGB(pixel.r, pixel.g, pixel.b));
        }
    }
}

One color is marked transparent and everything is fine. But for alpha-channel-textures, the best I got was:


gfx::TextureAccessorA32 accessor(texture, true);
for(unsigned int x = 0; x < (unsigned int)vSize.x; x++)
{
	for(unsigned int y = 0; y < (unsigned int)vSize.y; y++)
	{
		auto& pixel = accessor.GetPixel(x, y);

		const unsigned int invAlpha = 255 - pixel.a;
		SetPixel(hAndMaskDC, x, y, RGB(invAlpha, invAlpha, invAlpha));
		SetPixel(hXorMaskDC, x, y, RGB(pixel.r, pixel.g, pixel.b));
	}
}

Which doesn't even produce results near what I'm expecting. Anyone familiar with the algorithm I need here?

Advertisement

Why not use a specialized app for this? Does it really have to be generated "on-the-fly"? A few cursors only take a little disk space after all.

You don't use an bitwise mask for an alphablended cursor. Bitwise mask can only give you on/off transparency.

See http://support.microsoft.com/kb/318876 for example of how to make alphablended cursors.


Why not use a specialized app for this? Does it really have to be generated "on-the-fly"? A few cursors only take a little disk space after all.

It does not really have to be on-the-fly 100%, but I prefer the ease of using simple image-files rather than having to relate to external tools for creating the cursors. I also sort of have cross-plattform into mind, and though it is a bit pre-maturely I quess that doing so will make it easier when it comes to it.


You don't use an bitwise mask for an alphablended cursor. Bitwise mask can only give you on/off transparency.

See http://support.microsoft.com/kb/318876 for example of how to make alphablended cursors.

Thanks, thats what I've been looking for! Now I only need to tell microsoft to stop translating that article to german - poorly auto-translated article in my primary language is harder to read than english article biggrin.png

This topic is closed to new replies.

Advertisement