Jump to content
Gustav Schubert

FMX TEdit: how to prevent user from pasting a TBitmap ?

Recommended Posts

A minor problem: The TEdit may attempt to process clipboard content that was not put there for it to handle.

 

Design time steps:

 

1. New empty FMX Application
2. Add components
    Button1: TButton;
    Edit1: TEdit;
3. Do in FormCreate
    ReportMemoryLeaksOnShutdown := True;
    Edit1.Width := 400 // optional
4. Implement ButtonClick
   Copy a TBitmap to the clipboard.
   ( see test code below that copies a screenshot of the form )

 

Runtime steps:

 

  1.  Copy TBitmap to Clipboard. (click Button)
  2.  Set Focus to TEdit control. (tab to Edit)
  3. Paste from Clipboard. (via ctrl V or via context menu)
  4. Assert that Edit1.Text contains '(TBitmapSurface @'.
  5. Close App.
  6. Observe that there is a Memory leak.

 

Observations:

 

a)  (TBitmapSurface @ 0F83E7D0) appears as Edit1.Text

b)  Memory leak reported after Application shutdown

    29 - 36 bytes: TBitmapSurface x 2 // Pasted via Ctrl V
    29 - 36 bytes: TBitmapSurface x 3 // Pasted via Context Menu

 

Implemetation details of test form:

implementation

uses
  FMX.Platform;

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
  Edit1.Width := 400;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CopyBitmap;
end;

procedure TForm1.CopyBitmap;
var
  bmp: TBitmap;
begin
  bmp := TBitmap.Create(Round(ClientWidth), Round(ClientHeight));
  bmp.Clear(0);
  if bmp.Canvas.BeginScene then
  try
    PaintTo(bmp.Canvas);
  finally
    bmp.Canvas.EndScene;
  end;
  CopyBitmapToClipboard(bmp);
  bmp.Free;
end;

procedure TForm1.CopyBitmapToClipboard(ABitmap: TBitmap);
var
  Svc: IFMXClipboardService;
begin
  if not Assigned(ABitmap) then
    Exit;
  if TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, Svc) then
    Svc.SetClipboard(ABitmap);
end;

end.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×