I have this code to draw on Desktop's canvas:
procedure Paint; {Pseudo code}
begin
repeat
DrawOnWindow();
sleep(100);
Application.ProcessMessages;
until;
end;
function DrawOnWindow(Handle: HWND; X, Y: Integer; BMP : TBitmap): Boolean;
{ actual code }
var
Canvas : TCanvas;
DC : HDC;
begin
Result:= FALSE;
Assert(Handle > 0); // if change is possible then
try
DC := GetDC(Handle); // the dc is freed after repainting
Canvas := TCanvas.Create;
Canvas.Handle := DC;
Canvas.Draw(x, y, bmp);
Canvas.Free;
Result := TRUE;
ReleaseDC(Handle,DC);
except
end;
end;
The drawing worked fine. But it was freezing my program and I had to use Application.ProcessMessages to 'unfreeze' it. But Application.ProcessMessages was creating its own problems.
So I moved the code in a thread. Now it works for a while (< 1 minute) then the canvas is not painted anymore (but the thread is running).
- Why is this happening? Am I supposed to lock that canvas?
- An alternative question (path) would be: Should I move the painting code back in the main app/thread and paint from a TTimer (or better, a high precision timer)?