Multi-Device Apps and Clipboard Support

by Jan 31, 2017

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.

 

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: