Delphi XE8でFiremonkeyアプリケーションでクリップボードに文字列をコピーする

Delphi XE8でFiremonkeyアプリケーションでクリップボードに文字列をコピーする方法を紹介します。

クリップボードを使用するには、IFMXClipboardServiceインターフェースを使用します。

アプリケーションを実行しているプラットフォームがIFMXClipboardServiceを利用可能かどうかを調べます。

var
  ClipboardService: IFMXClipboardService;

if TPlatformServices.Current.SupportsPlatformService(
  IFMXClipboardService, IInterface(ClipboardService)) then
begin
  //IFMXClipboardServiceが利用な可能なとき
end;

IFMXClipboardServiceが利用可能であれば、変数ClipboardServiceにIFMXClipboardServiceのインスタンスが入っています。

クリップボードに文字列をコピーするには、IFMXClipboardServiceのSetClipboardメソッドを使用します。

ClipboardService.SetClipboard('コピーする文字列');

クリップボードに文字列をコピーするコードは次のようになります。

uses FMX.Platform;

procedure TForm1.Button1Click(Sender: TObject);
var
  ClipboardService: IFMXClipboardService;
begin
  if TPlatformServices.Current.SupportsPlatformService(
    IFMXClipboardService, IInterface(ClipboardService)) then
  begin
    ClipboardService.SetClipboard('コピーする文字列');
  end;
end;

次のサンプルアプリケーションでは、アプリケーションを実行しているプラットフォームで利用できるディレクトリのパスを取得して、クリップボードにコピーします。

フォームにボタンコンポーネント(TButton)とメモコンポーネント(TMemo)を配置します。

03

フォームのOnCreateイベントと、ボタンのOnClickイベントを記述します。

uses System.IOUtils, FMX.Platform;

procedure TForm1.Button1Click(Sender: TObject);
var
  ClipboardService: IFMXClipboardService;
begin
  Memo1.SelectAll;

  if TPlatformServices.Current.SupportsPlatformService(
    IFMXClipboardService, IInterface(ClipboardService)) then
  begin
    ClipboardService.SetClipboard(Memo1.Text);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Lines.Add('System.IOUtils.TPath.GetLibraryPath='+System.IOUtils.TPath.GetLibraryPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetDocumentsPath='+System.IOUtils.TPath.GetDocumentsPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetHomePath='+System.IOUtils.TPath.GetHomePath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetSharedAlarmsPath='+System.IOUtils.TPath.GetSharedAlarmsPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetPublicPath='+System.IOUtils.TPath.GetPublicPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetSharedDocumentsPath='+System.IOUtils.TPath.GetSharedDocumentsPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetDownloadsPath='+System.IOUtils.TPath.GetDownloadsPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetMoviesPath='+System.IOUtils.TPath.GetMoviesPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetMusicPath='+System.IOUtils.TPath.GetMusicPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetPicturesPath='+System.IOUtils.TPath.GetPicturesPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetRingtonesPath='+System.IOUtils.TPath.GetRingtonesPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetSharedDownloadsPath='+System.IOUtils.TPath.GetSharedDownloadsPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetSharedMoviesPath='+System.IOUtils.TPath.GetSharedMoviesPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetSharedMusicPath='+System.IOUtils.TPath.GetSharedMusicPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetSharedCameraPath='+System.IOUtils.TPath.GetSharedCameraPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetSharedPicturesPath='+System.IOUtils.TPath.GetSharedPicturesPath);
  Memo1.Lines.Add('System.IOUtils.TPath.GetSharedRingtonesPath='+System.IOUtils.TPath.GetSharedRingtonesPath);
end;

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください