在最顶部、任何地方绘制类似系统的光标
我需要绘制一个类似系统的光标,我可以简单地控制其位置。 换句话说,我需要绘制一个看起来像系统光标的透明图像,并且需要将其渲染在所有其他窗口的顶部。 我尝试了多种方法,但它们似乎都有一些缺点。
我发现可以使用 LoadImage() 加载光标图像并传递资源 OCR_NORMAL 并将其转换为 HBITMAP。
HICON NormalCursor = (HICON)LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
然后获取“桌面”HDC
hDC = GetDC(NULL);
然后我可以尝试使用 DrawIconEx() 绘制它
DrawIconEx(hDC, (int)x, 0, NormalCursor, 0, 0, NULL, NULL, DI_DEFAULTSIZE | DI_NORMAL);
DI_NORMAL 标志应该结合 DI_IMAGE 和DI_MASK 标志给我一个透明的图像/图标/光标,但这是我在桌面上的结果:
更不用说如果它移动它会产生痕迹。
通过使用 SetLayeredWindowAttributes 来制作透明窗口,如下所示:
SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 50, LWA_COLORKEY);
并且将窗口的背景颜色设置为黑色,我可以从窗口中删除背景。但由于基于颜色进行 Alpha 处理,我的光标图标周围出现了丑陋的黑色像素。
除了使用颜色遮罩之外,我可以通过其他方式使窗口的背景透明吗?
如何在所有窗口顶部正确绘制透明光标?
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您制作自己的窗口,并执行类似 中描述的操作http://www.codeproject.com/KB/GDI-plus/CsTranspTutorial3.aspx。它是用 C# 编写的,但大部分只是 win32 调用。它在可变透明度方面也做得很好,而不仅仅是 0%/100%。
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%.
光标的轮廓不是黑色的吗?问题只是因为你也让轮廓变得透明吗?为什么不将透明度颜色(以及窗口的背景颜色)更改为黑色或白色以外的任何颜色?
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?