Recent

Author Topic: Copy contents of a TPanel to Clipboard  (Read 3869 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 475
Copy contents of a TPanel to Clipboard
« on: November 24, 2019, 03:57:34 am »
Platform:  Laz 1.8.4, FPC 3.0.4,  Dev OS: Windows Server 2016  Target OS:  Same as Dev OS.

Background:  I have a TPanel with two TImage components on it, displaying two photos (one in each TImage) 
Goal:  I want to copy to clipboard, the full contents of the TPanel with both images, into one image that I can then paste into MS Paint

So far, I have found only this:

var
  r : Trect;
  ScreenCanvas: TCanvas;
  bmp: TBitmap;
begin
  r := pnlPrcClnFotoVwrLftRgtContainer.BoundsRect;
  MapWindowPoints( Handle, HWND_DESKTOP, r, 2 );
  ScreenCanvas := TCanvas.Create;
  try
    ScreenCanvas.Handle := GetDC( 0 );
    try
      bmp:= TBitmap.Create;
      Try
        bmp.Width := Panel1.Width;
        bmp.Height:= Panel1.Height;
        bmp.Canvas.CopyRect( bmp.Canvas.ClipRect, Screencanvas, r );
        Clipboard.Assign(bmp);
      Finally
        bmp.free;
      End;
    finally
      ReleaseDC( 0, ScreenCanvas.Handle );
      ScreenCanvas.Handle := 0;
    End;
  finally
    ScreenCanvas.Free;
  end;
end;

Problem:  MapWindowPoints   is not recognized by Lazarus.  Will it cause problems if I simply include the unit

pmwin

in my Lazarus USES clause?

Alternatively, is there a better, simpler, "Lazarus" way of doing this?






ASerge

  • Hero Member
  • *****
  • Posts: 2510
Re: Copy contents of a TPanel to Clipboard
« Reply #1 on: November 24, 2019, 01:35:20 pm »
Alternatively, is there a better, simpler, "Lazarus" way of doing this?
Code: Pascal  [Select][+][-]
  1. uses Clipbrd;
  2.  
  3. procedure CopyPanelToClipboard(Panel: TPanel);
  4. var
  5.   B: TBitmap;
  6.   R: TRect;
  7. begin
  8.   B := TBitmap.Create;
  9.   try
  10.     R := Panel.ClientRect;
  11.     B.Canvas.CopyRect(R, Panel.Canvas, R);
  12.     B.SaveToClipboardFormat(CF_Bitmap);
  13.   finally
  14.     B.Free;
  15.   end;
  16. end;

RedOctober

  • Sr. Member
  • ****
  • Posts: 475
Re: Copy contents of a TPanel to Clipboard
« Reply #2 on: November 24, 2019, 06:00:27 pm »
@ASerge   Hi Serge, thank you for this simple snippet.  I think something is missing in my source code bc although the procedure runs without error, I cannot paste what was copied into MS Paint on Windows.  Is there another line I have to include?  I'm on Laz 1.8.4 FPC 3.0.4

Thaddy

  • Hero Member
  • *****
  • Posts: 19398
  • Glad to be alive.
Re: Copy contents of a TPanel to Clipboard
« Reply #3 on: November 24, 2019, 06:12:21 pm »
I'm on Laz 1.8.4
Well, we are at 2.0.6, that may be the difference.
objects are fine constructs. You can even initialize them with constructors.

Eleazar

  • New Member
  • *
  • Posts: 26
Re: Copy contents of a TPanel to Clipboard
« Reply #4 on: June 05, 2020, 10:37:15 pm »
I tried this code as well and initially it did not work. However, by adding a line that sets the size of the bitmap it does:
Code: Pascal  [Select][+][-]
  1.     R := Panel.ClientRect;
  2.     B.SetSize(R.Width,R.Height);
  3.     B.Canvas.CopyRect(R, Panel.Canvas, R);

jamie

  • Hero Member
  • *****
  • Posts: 7830
Re: Copy contents of a TPanel to Clipboard
« Reply #5 on: June 05, 2020, 11:41:40 pm »
and you have this

Code: Pascal  [Select][+][-]
  1.   B  := TBitmap.Create;
  2.   B.SetSize(panel1.Width, Panel1.Height);
  3.   Panel1.PaintTo(B.Canvas,0,0);
  4.   B.SaveToClipboardFormat(CF_BITMAP);
  5.   B.Free;
  6.                                    
  7.  

The only true wisdom is knowing you know nothing

Eleazar

  • New Member
  • *
  • Posts: 26
Re: Copy contents of a TPanel to Clipboard
« Reply #6 on: June 05, 2020, 11:50:58 pm »
complete I have it like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CopyTreeviewToClipboard(Panel: Tpanel);
  2. var
  3.   B: TBitmap;
  4.   R: TRect;
  5. begin
  6.   B := TBitmap.Create;
  7.   try
  8.     R := Panel.ClientRect;
  9.     B.SetSize(R.Width,R.Height);
  10.     B.Canvas.CopyRect(R, Panel.Canvas, R);
  11.     B.SaveToClipboardFormat(CF_Bitmap);
  12.   finally
  13.     B.Free;
  14.   end;
  15. end;

jamie

  • Hero Member
  • *****
  • Posts: 7830
Re: Copy contents of a TPanel to Clipboard
« Reply #7 on: June 05, 2020, 11:59:47 pm »
keep in mind if the panel is covered it may not fully capture that way..
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018