1. 程式人生 > >[FMX]在你的跨平臺應用中使用剪貼板進行復制粘貼

[FMX]在你的跨平臺應用中使用剪貼板進行復制粘貼

ble begin itl 自定義格式 aps nac code min -m

[FMX]在你的跨平臺應用中使用剪貼板進行復制粘貼

2017-08-10 ? Android、C++ Builder、Delphi、iOS、教程 ? 暫無評論 ? swish ?瀏覽 516 次

VCL 中如何使用剪貼板咱就不說了,FMX 做為一個新的框架,提供了跨平臺的剪貼板支持。FMX 對剪貼板的支持來自兩個接口:

  • IFMXClipboardService:位於 FMX.Platform.pas 中
    1 2 3 4 5 6 7 8 9 10 11 IFMXClipboardService = interface(IInterface) [‘{CC9F70B3-E5AE-4E01-A6FB-E3FC54F5C54E}‘]
    /// <summary> /// Gets current clipboard value /// </summary> function GetClipboard: TValue; /// <summary> /// Sets new clipboard value /// </summary> procedure SetClipboard(Value: TValue); end;
  • IFMXExtendedClipboardService:位於 FMX.Clipboard.pas 中
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 IFMXExtendedClipboardService = interface(IFMXClipboardService) [‘{E96E4776-8234-49F9-B15F-301074E23F70}‘] function HasText: Boolean; function GetText: string; procedure SetText(const Value: string); function HasImage: Boolean; function GetImage: TBitmapSurface; procedure SetImage(const Value: TBitmapSurface);
    procedure RegisterCustomFormat(const AFormatName: string); function IsCustomFormatRegistered(const AFormatName: string): Boolean; procedure UnregisterCustomFormat(const AFormatName: string); function HasCustomFormat(const AFormatName: string): Boolean; function GetCustomFormat(const AFormatName: string; const AStream: TStream): Boolean; procedure SetCustomFormat(const AFormatName: string; const AStream: TStream); end;

很明顯,第二種更符合VCL中TClipboard的使用習慣。而且如果要使用自定義格式的內容,則必需使用第二種格式,第一種格式的支持情況如下(以10.2 為準,未來版本請自行查看):

  1. Windows 平臺(FMX.Clipboard.Win.pas):文本、位圖
  2. Android 平臺(FMX.Clipboard.Android.pas):文本
  3. iOS 平臺(FMX.Clipboard.iOS.pas):文本、位圖
  4. OSX 平臺(FMX.Clipboard.Mac.pas):文本、位圖

註意一下,支持位圖的平臺,實際上 TValue 支持的是 TBitmapSurface,當然設置值時也支持 TBitmap ,但 GetClipboard 返回的就只是 TBitmapSurface 類型的對象了。

好了,回歸正轉,說一下基本的使用步驟:

  1. 引用 fmx.platform 單元,如果使用第二個接口,同時使用 fmx.clipboard 單元。
  2. 用 TPlatformServices.Current.SupportsPlatformService 函數來獲取剪貼板服務接口實例。
  3. 調用獲取的接口實例的相關函數來執行相關的功能。

一個簡單的示例:

1 2 3 4 5 6 7 8 9 procedure TForm1.Button1Click(Sender: TObject); var AClipboard:IFMXClipboardService; begin if TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService,AClipboard) then begin AClipboard.SetClipboard(‘Hello,world from delphi‘); end; end;

至於其它的幾個接口,大家看相關接口的幫助就可以了。

[FMX]在你的跨平臺應用中使用剪貼板進行復制粘貼