Canvas does not allow drawing

Bob picture Bob · Apr 4, 2013 · Viewed 14.1k times · Source

I want to Draw a Screenshot from the entire screen to a TForm1 Canvas.

This code works well in Delphi XE3

procedure TForm1.Button1Click(Sender: TObject);
var
  c,scr: TCanvas;
  r,r2: TRect;
begin

  c := TCanvas.Create;
  scr := TCanvas.Create;
  c.Handle := GetWindowDC(GetDesktopWindow);
  try

    r := Rect(0, 0, 200, 200);
    form1.Canvas.CopyRect(r, c, r);

  finally
    ReleaseDC(0, c.Handle);
    c.Free;
  end;

Now I want to copy the screenshot to another canvas first. Is there a way to do this without getting this error?

procedure TForm1.Button1Click(Sender: TObject);
var
  c,scr: TCanvas;
  r,r2: TRect;
begin

  c := TCanvas.Create;
  scr := TCanvas.Create;
  c.Handle := GetWindowDC(GetDesktopWindow);
  try

    r := Rect(0, 0, 200, 200);

    scr.CopyRect(r,c,r); <-- Error, canvas does not allow drawing
    form1.Canvas.CopyRect(r, scr, r); <-- Error, canvas does not allow drawing

  finally
    ReleaseDC(0, c.Handle);
    c.Free;
  end;

Answer

bummi picture bummi · Apr 4, 2013

If you need to work with an additional canvas you will have to assign a HDC e.g.

var
  WindowHandle:HWND;
  ScreenCanvas,BufferCanvas: TCanvas;
  r,r2: TRect;
  ScreenDC,BufferDC :HDC;
  BufferBitmap : HBITMAP;
begin
  WindowHandle := 0;
  ScreenCanvas := TCanvas.Create;
  BufferCanvas := TCanvas.Create;

  ScreenDC:=GetWindowDC(WindowHandle);
  ScreenCanvas.Handle := ScreenDC;

  BufferDC := CreateCompatibleDC(ScreenDC);
  BufferCanvas.Handle := BufferDC;
  BufferBitmap := CreateCompatibleBitmap(ScreenDC,
                     GetDeviceCaps(ScreenDC, HORZRES),
                     GetDeviceCaps(ScreenDC, VERTRES));
  SelectObject(BufferDC, BufferBitmap);

  try
    r := Rect(0, 0, 200, 200);
    BufferCanvas.CopyRect(r,ScreenCanvas,r);
    form1.Canvas.CopyRect(r, BufferCanvas, r);
  finally
    ReleaseDC(WindowHandle, ScreenCanvas.Handle);
    DeleteDC(BufferDC);
    DeleteObject(BufferBitmap);
    BufferCanvas.Free;
    ScreenCanvas.Free;
  end;
end;