Author: Marko Von Richards
We have a lot of great demos to help you get started with RAD Studio. Today’s blog post focuses on our CopyPaste FireMonkey demo.
The CopyPaste sample demonstrates how to create applications that use the system’s clipboard to copy and paste text or images. The sample uses IFMXClipboardService to interact with the system clipboard. The SetClipboard method is used to put data into the system clipboard and the GetClipboard method is used to return data from the system clipboard.
Windows, macOS and iOS platforms provide copy/paste of both text and images. The Android clipboard does not support images. To allow users to copy and paste images between your own Android applications, you can use a custom format instead.
The CopyPaste sample leverages FMX.Controls.TControl.MakeScreenshot. MakeScreenshot creates a new TBitmap, draws on it the image of the current control by calling PaintTo, and returns it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
procedure TCopyPasteDemo.CopyButtonClick(Sender: TObject); var Svc: IFMXClipboardService; Image: TBitmap; begin if TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, Svc) then if TextRadioButton.IsChecked then Svc.SetClipboard(Edit1.Text) else begin Image := TextBorder.MakeScreenshot; try Svc.SetClipboard(Image); finally Image.Free; end; end; end; procedure TCopyPasteDemo.PasteButtonClick(Sender: TObject); var Svc: IFMXClipboardService; Value: TValue; Bitmap: TBitmap; begin if TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, Svc) then begin Value := Svc.GetClipboard; if not Value.IsEmpty then begin if Value.IsType<string> then begin PasteLabel.Text := Value.ToString; PasteImage.Bitmap := nil; end else if Value.IsType<TBitmapSurface> then try PasteLabel.Text := string.Empty; Bitmap := TBitmap.Create; try Bitmap.Assign(Value.AsType<TBitmapSurface>); PasteImage.Bitmap := Bitmap; finally Bitmap.Free; end; finally Value.AsType<TBitmapSurface>.Free; end; end; end; end; |
See the demo in action below:
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition