Recent

Author Topic: Copy Application Image To Clipboard  (Read 432 times)

Bazzao

  • Full Member
  • ***
  • Posts: 145
  • Pies are squared.
Copy Application Image To Clipboard
« on: June 22, 2019, 05:16:45 am »
I can copy a Form canvas to clipboard, but it does not include the header.

How do I copy the Application as shown to the the clipboard (ie coding equivalent of Alt-Printscrn)?

Bazza
Bazza

Lazarus 1.6.4 r54278 FPC 3.0.2 x86_64-win64-win32/win64
Windows 10.

RAW

  • Hero Member
  • *****
  • Posts: 794
Re: Copy Application Image To Clipboard
« Reply #1 on: August 21, 2019, 06:06:36 am »
On Windows: Try this ...
(found it in a german thread, seems to work even if the window is hidden, take a look at PrintWindow ... needs XP/Server 2003 or newer)

Code: Pascal  [Select]
  1. UNIT Unit1;
  2. {$MODE DELPHI}
  3. INTERFACE
  4.  
  5. USES
  6.   Windows,
  7.   Graphics,
  8.   Forms,
  9.   ExtCtrls;
  10.  
  11. TYPE
  12.   TForm1 = Class(TForm)
  13.     Image1: TImage;
  14.     Procedure Image1Click(Sender: TObject);
  15.   End;
  16.  
  17. Var
  18.   Form1: TForm1;
  19.  
  20. IMPLEMENTATION
  21. {$R *.LFM}
  22.  
  23. Function ScrShotWnd(wnd: HWND; Const bmp: TBitmap): Boolean;
  24. Var
  25.  r: TRect;
  26.  hDLL: THandle;
  27.  PrintWindow: Function(aWnd: HWND; hDC: HDC; nFlags: UINT): BOOL; Stdcall;
  28. Begin
  29.   Result:= False;
  30.   hDLL:= GetModuleHandle(user32);
  31.   If hDLL <> 0
  32.   Then
  33.    Begin
  34.      @PrintWindow:= GetProcAddress(hDLL, 'PrintWindow');
  35.      If @PrintWindow <> Nil
  36.      Then
  37.       Begin
  38.         GetWindowRect(wnd, r);
  39.         bmp.Width := r.Right -r.Left;
  40.         bmp.Height:= r.Bottom-r.Top;
  41.         bmp.Canvas.Lock;
  42.         Try
  43.           Result:= PrintWindow(wnd, bmp.Canvas.Handle, 0);
  44.         Finally
  45.           bmp.Canvas.Unlock;
  46.         End;
  47.       End;
  48.    End;
  49. End;
  50.  
  51. Procedure TForm1.Image1Click(Sender: TObject);
  52. Var
  53.  Wnd: THandle;
  54. Begin
  55.   Wnd:= FindWindow('AkelPad4', Nil);
  56.   ScrShotWnd(Wnd, Image1.Picture.Bitmap);
  57.   Image1.Invalidate;
  58.   SetBounds(0, 0, Image1.Picture.Bitmap.Width, Image1.Picture.Bitmap.Height);
  59. End;
  60.  
  61. END.
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.4 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1

RAW

  • Hero Member
  • *****
  • Posts: 794
Re: Copy Application Image To Clipboard
« Reply #2 on: August 21, 2019, 07:06:51 am »
Or this ...
Code: Pascal  [Select]
  1. procedure TForm1.FormClick(Sender: TObject);
  2. begin
  3.   KEYBD_Event(VK_SNAPSHOT, 1, 0, 0);
  4. end;  
  5.  
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.4 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1

Bazzao

  • Full Member
  • ***
  • Posts: 145
  • Pies are squared.
Re: Copy Application Image To Clipboard
« Reply #3 on: August 22, 2019, 10:50:52 am »
I tried

Quote
KEYBD_Event(VK_SNAPSHOT, 1, 0, 0);

and after adding the necessary uses, it worked first go.

Very good RAW.

Thanks,

Bazza

Lazarus 1.6.4 r54278 FPC 3.0.2 x86_64-win64-win32/win64
Windows 10.