如何清除 TCanvas?或者任何其他解决方案
我正在使用这个 TCanvas 为我的鼠标绘制光标
Canv := TCanvas.Create;
Canv.Handle := GetWindowDC(0);
...... 对于每个鼠标事件执行以下
Bitmap:=TBitmap.Create;
CursorInfo.cbSize := sizeof(CursorInfo);
GetCursorInfo(CursorInfo);
Bitmap.Width := 32;
Bitmap.Height := 32;
Bitmap.Transparent:=true;
DrawIconEx(Bitmap.Canvas.Handle, 0,0, CursorInfo.hCursor, 32,32, 0,0, DI_NORMAL) ;
Bitmap.Canvas.Brush.Color := RGB(250,250,250);
Bitmap.Canvas.FloodFill(31,0, clWhite, fsSurface);
Bitmap.Canvas.FloodFill(0,0, clWhite, fsSurface);
currentX:=getcurrentxpos;
currentY:=getcurrentypos;
Canv.Draw(currentX,currentY,Bitmap);
Bitmap.Free;
操作问题是不仅仅是显示各个光标,它使 鼠标轨迹。我可以在鼠标移动时清除整个画布吗? (虽然听起来不是一个好主意)。也许我可以通过执行该代码的相反操作来清除之前的 Canv.Draw
(如果可能的话)?关于如何显示光标有什么建议吗?
编辑: 在设置位图宽度和高度后尝试插入另一个 Canv.Draw(currentX,currentY,Bitmap);
...现在的问题是我有一条白色轨迹(而不是鼠标轨迹),干净多了,但还是不好。
I'm using this TCanvas to draw cursors for my mice
Canv := TCanvas.Create;
Canv.Handle := GetWindowDC(0);
....
For every mice event do the following
Bitmap:=TBitmap.Create;
CursorInfo.cbSize := sizeof(CursorInfo);
GetCursorInfo(CursorInfo);
Bitmap.Width := 32;
Bitmap.Height := 32;
Bitmap.Transparent:=true;
DrawIconEx(Bitmap.Canvas.Handle, 0,0, CursorInfo.hCursor, 32,32, 0,0, DI_NORMAL) ;
Bitmap.Canvas.Brush.Color := RGB(250,250,250);
Bitmap.Canvas.FloodFill(31,0, clWhite, fsSurface);
Bitmap.Canvas.FloodFill(0,0, clWhite, fsSurface);
currentX:=getcurrentxpos;
currentY:=getcurrentypos;
Canv.Draw(currentX,currentY,Bitmap);
Bitmap.Free;
The problem is instead of just showing the individual cursors, it makes mouse trails. Can I clear the whole Canvas evertime a mouse moves? (doesn't sound like a good idea though). Maybe I could clear my previous Canv.Draw
by doing the reverse of that code (if it is possible)? Any suggestions as to how I can show the cursors?
EDIT:
tried inserting another Canv.Draw(currentX,currentY,Bitmap);
just after setting the bitmap width and height...and now the problem is I have a white trail (rather than a mouse trail), much cleaner but still no good.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在桌面上画画,这是你永远不应该做的事情,因为它不可靠。据我了解,您希望找到一种方法在桌面上绘制鼠标光标,当鼠标再次移动时,“撤消”上次绘制并在新坐标处重新绘制鼠标。想象一下:您将鼠标移动到备忘录框上方的某个位置,将手移到键盘上,输入一些内容,然后再次移动鼠标。老鼠下面的图像发生了变化!
您可以做什么:创建一个鼠标光标形状的表单,有已知的技术可以做到这一点。让你的伪光标保持在顶部(你也会遇到一些问题,因为 Windows 不再喜欢保持在顶部的东西)。这并不容易,但它是可以管理的,而且是按规则行事。
对到目前为止所得到的内容进行一些代码审查,因为我发现了我认为这是一个错误的地方,而你应该知道这一点。修复此问题不足以解决您的问题,您需要停止在桌面上绘图:
不要释放保存透明光标图像的位图,在应用程序的生命周期中保留:您将保存两者内存和CPU。这对于需要对老鼠的运动做出反应的东西来说至关重要。
You're drawing on the DESKTOP, and that's something you should never do, because it's unreliable. As I understand it, you're hoping to find a way to paint your mouse cursor on the desktop, and when the mice moves again, "undo" your last paint and repaint the mice at the new coordinates. Imagine this: You move the mice somewhere over a Memo box, move your hands to the keyboard, type something and then move the mouse again. The image under the mice changed!
What you can do: Create a mouse-cursor-shaped form, there are known techniques to do that. Make your pseudo-cursor stay on top (you'll get into a bit of problem with that too, because Windows no longer likes stay-on-top things). This is not going to be easy, but it's manageable and it's PLAYING BY THE RULES.
A little code review on what you've got so far, because I spotted what I think it's a mistake, and you should know about. Fixing this is not enough to fix your problem, you need to stop drawing onto the desktop:
Don't free the Bitmap that holds the transparent cursor image, keep if for the life of the application: You'll save both RAM and CPU. This is critical in something that needs to react to the movement of a mice.
我将向您展示一种方法,该方法与我之前建议您的方法不同。
您可以使用
Canvas.CopyRect
将画布存储在某个临时画布中。当鼠标第一次悬停在画布上时。然后,当鼠标移动时,首先将临时画布复制到目标画布,然后绘制光标。
I will show you one way, which is different from what I previously suggest you.
You store your canvas using
Canvas.CopyRect
in some temporary canvas. when your mouse is first hover on your canvas.Then, when your mouse is moved, first copy the temporary canvas to your destination canvas then draw your cursor.
有一个参考图像(这就是画布上没有绘制鼠标光标时的样子),将其复制到临时位图,然后在该临时位图上绘制光标。最后,在画布上绘制图像。
它还将允许您非常轻松地拥有更复杂的临时覆盖:您只需根据它们的 Z 顺序将它们绘制在参考位图的顶部即可。由于它们不接触背景,因此您不必担心清除鬼影。
Have a reference image (which is what your canvas should looks like without the mouse cursor painted on it), copy it to a temporary bitmap and then draw your cursor on that temp bitmap. Finally, draw your image on the canvas.
It also will allow you to have more complex temporary overlays very easily: you just have to draw them on top of the reference bitmap according to their Z-order. Since they aren't touching the background, you don't have to worry about clearing the ghosts.