介绍如何在Delphi XE8中使用Firemonkey应用程序将文本复制到剪贴板。
要使用剪贴板,请使用IFMXClipboardService接口。
检查运行应用程序的平台是否可以使用IFMXClipboardService。
var
ClipboardService: IFMXClipboardService;
if TPlatformServices.Current.SupportsPlatformService(
IFMXClipboardService, IInterface(ClipboardService)) then
begin
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)。
描述表单的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;