Search code examples
delphicursormultiple-mice

How can I clear TCanvas? Or any other solution


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.


Solution

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