Multi-Device Apps and Clipboard Support

Posted by on in Programming

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:

 

 



About
Gold User, Rank: 5, Points: 558
Senior Product Manager, RAD Studio

Comments

  • Amiga S5424
    Amiga S5424 Friday, 10 March 2017

    Thanks for the blog post about Clipboard usage.

    But could you tell us how it's possible to delete the clipboard entry I've used? Android uses more than 15 clipboards and for security reasons it would be nice to know how I can delete my clipboard entry after usage...

    Many thanks for your support.

  • Robert K33373
    Robert K33373 Tuesday, 7 March 2017

    Hi, I have been doing Drag / Drop for sometime using the Drag Drop utilities written by Anders Melander but those have been unsupported by Anders since 2010. I and others on the web have picked up the code through the years but these are still Windows Only. These tools are quite complex but they handle a large class of Drag Drop types and issue. Its not just simple text fields. You have files, URLs, all kind of things.

    I would be very curious as to the details of how Drag Drop is handled in Fire Monkey give the wide range of target devices. Given the complexity I have seen with Anders code I am skeptical about the "Depth" of the Fire Monkey solution. I would love to know.

    Thanks,
    Bob Kondner

  • Maciej D31717
    Maciej D31717 Thursday, 9 February 2017

    Thank You. Could you write something about working with huge amount of data in FMX? (without TDBGrid class). The LiveBindings is completely unusable (speed, stability).

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: