views:

50

answers:

2

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:

Image

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?

A: 

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%.

Reinderien
I'd like to avoid MFC and magic sorcery like "this.panel1.BackColor = System.Drawing.Color.Transparent" in that example.
sippeangelo
A: 

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?

brian
Yes, the outline is black, but I get extra black pixels because the cursor has semi-transparent pixels at the very edge. If I had the background red and I had red as they key, I would get red trash pixels around my cursor.
sippeangelo
Ah, I didn't realize the cursor was doing any antialiasing. In that case, you will probably need to use GDI+.
brian