2
votes

I need to draw a system-like cursor that I simply can control the position of. In other words, I need to draw a transparent image that looks just like the system cursor and I need it to be rendered on top of all other windows. I've tried multiple approaches, but they all seem to have some downside.

I've figured out that I can load the cursor image by using LoadImage() and passing the resource OCR_NORMAL and casting it into a HBITMAP.

HICON NormalCursor = (HICON)LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);

Then getting the "desktop" HDC

hDC = GetDC(NULL);

Then I can try to draw it using DrawIconEx()

DrawIconEx(hDC, (int)x, 0, NormalCursor, 0, 0, NULL, NULL, DI_DEFAULTSIZE | DI_NORMAL);

The DI_NORMAL flag is supposed to combine the DI_IMAGE & DI_MASK flags giving me a transparent image/icon/cursor, but this is my result on the desktop:

enter image description here

Not to mention that if it moves it creates trails.

By making a transparent window using SetLayeredWindowAttributes like this:

SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 50, LWA_COLORKEY);

And having the background color of my window to be black, I can remove the background from the window. But due to doing alpha based on a color I get ugly black pixels around my cursor icon.

Can I make the background of a window transparent in some other way than using a color mask?

How do I draw a transparent cursor on top of all windows properly?

2
I find it sad that it has to be this hard. Who the hell thought of hardware cursors and why does it still live in Windows today? - sippeangelo
Can I ask why you are not able just to use the system cursor itself? - Ian Goldby

2 Answers

0
votes

I would recommend that you do make your own window, and do something like what's described at http://www.codeproject.com/KB/GDI-plus/CsTranspTutorial3.aspx . It's in C#, but most of it is just win32 calls. It does a nice job of variable transparency, too, not just 0%/100%.

0
votes

Isn't the outline of the cursor black? Is the problem just that you're making the outline transparent too? Why don't you just change the transparency color (and the background color of the window) to anything other than black or white?