0

In a Delphi 10.4.2 32-bit VCL Application in Windows 10, I draw on the desktop with this code:

var FTempCanvas := TCanvas.Create;
try
  // get the device context of the desktop and assign it to the canvas.
  // Then all drawing on the canvas will appear on the desktop until refreshing the desktop:
  FTempCanvas.Handle := Winapi.Windows.GetDC(0);
  try
    // drawing on the desktop canvas here
  finally
    Winapi.Windows.ReleaseDC(0, FTempCanvas.Handle);
  end;
finally
  FTempCanvas.Handle := 0;
  FTempCanvas.Free;
end;

How can I REMOVE and CLEANUP all the drawings without leaving any traces?

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • Have you tried `RedrawWindow(0, nil, 0, RDW_ERASE or RDW_INVALIDATE or RDW_ALLCHILDREN);`? – Andreas Rejbrand Jul 23 '21 at 11:46
  • @AndreasRejbrand Yes, I have tried it and it works. But the mouse cursor erases part of my drawings when passing over them. I will try to free the canvas (and release the DC) only when I erase the drawings. As for the warning of IInspectable, in a second test, I will create a canvas without dependence on Desktop DC but only with the size of the Desktop. – user1580348 Jul 23 '21 at 12:17
  • @IInspectable You are right. I will now test a canvas without dependence on the device context of the Desktop, but only the size of the Desktop. – user1580348 Jul 23 '21 at 12:38
  • 3
    @user1580348: Yes, that is expected. IInspectable is of course absolutely right: Windows owns the desktop and can redraw it any time it sees fit (just like a VCL developer can redraw his or her form anytime (s)he wants to). When the cursor enters a desktop icon (say), Windows will naturally redraw (at least parts of) the desktop to display the hover effect on this icon. You cannot prevent Windows from doing that. Depending on your goal, you might however place a semi-transparent form on top of the desktop and draw on that. – Andreas Rejbrand Jul 23 '21 at 13:04
  • I have completely given up the idea to draw on the desktop. Now I use a TRANSPARENT CLICK-THROUGH window and place it over the target window as detailed in this answer: https://stackoverflow.com/questions/68467642/problems-when-drawing-a-frame-around-a-window/68471648#68471648 – user1580348 Jul 23 '21 at 15:30

0 Answers0