Lazarus

Programming => Graphics and Multimedia => Graphics => Topic started by: Bazzao on June 22, 2019, 05:16:45 am

Title: Copy Application Image To Clipboard
Post by: Bazzao 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
Title: Re: Copy Application Image To Clipboard
Post by: RAW 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.
Title: Re: Copy Application Image To Clipboard
Post by: RAW 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.  
Title: Re: Copy Application Image To Clipboard
Post by: Bazzao 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,