Mega Code Archive

 
Categories / Delphi / Examples
 

Working with the clipboard

Title: Working with the clipboard Question: How to get a delphi form as bitmap and copy it into the clipboard ? How to copy the whole screen into the clipboard ? Answer: This example uses an image, a button, and a shape component on a form. When the user clicks the button, an image of the form is stored in the FormImage variable and copied to the Clipboard. Then image of the form in then copied back to the image component, producing an interesting result, especially if the button is clicked multiple times. procedure TForm1.Button1Click(Sender: TObject); var FormImage: TBitmap; begin FormImage := GetFormImage; try Clipboard.Assign(FormImage); Image1.Picture.Assign(Clipboard); finally FormImage.Free; end; end; procedure TForm1.FormCreate(Sender: TObject); begin Shape1.Shape := stEllipse; Shape1.Brush.Color := clLime; Image1.Stretch := True; end; The following copy copies the whole screen into the clipboard: procedure CopyScreenToClipboard; var dx,dy : integer; hSourcDC,hDestDC, hBM, hbmOld : THandle; begin dx := screen.width; dy := screen.height; hSourcDC := CreateDC('DISPLAY',nil,nil,nil); hDestDC := CreateCompatibleDC(hSourcDC); hBM := CreateCompatibleBitmap(hSourcDC, dx, dy); hbmold:= SelectObject(hDestDC, hBM); BitBlt(hDestDC, 0, 0, dx, dy, hSourcDC, 0, 0, SRCCopy); OpenClipBoard(form1.handle); EmptyClipBoard; SetClipBoardData(CF_Bitmap, hBM); CloseClipBoard; SelectObject(hDestDC,hbmold); DeleteObject(hbm); DeleteDC(hDestDC); DeleteDC(hSourcDC); end;