2013-11-12

Hack TPanel to use VCL and Firemonkey together

Put a panel on VCL form, then assign the form property to Firemonkey Form object.
Here is the code to hack TPanel:
unit FMXAdapter;

interface

uses
  FMX.Forms,
  Vcl.ExtCtrls;

type
  TPanel = class(Vcl.ExtCtrls.TPanel)
  private
    FForm: TCommonCustomForm;
    procedure ResizeForm;
    procedure SetForm(const AForm: TCommonCustomForm);
  protected
    procedure Resize; override;
  public
    property Form: TCommonCustomForm read FForm write SetForm;
  end;

implementation

uses
  Types,
  Winapi.Windows, System.SysUtils,
  FMX.Platform.Win;


{ TPanel }

procedure TPanel.Resize;
begin
  inherited;
  ResizeForm();
end;

procedure TPanel.ResizeForm;
begin
  if Assigned(FForm) then
  begin
    FForm.SetBounds(BorderWidth, BorderWidth, ClientWidth + BorderWidth, ClientHeight + BorderWidth);
  end;
end;

procedure TPanel.SetForm(const AForm: TCommonCustomForm);
begin
  FForm := AForm;

  FForm.BorderIcons := [];
  FForm.BorderStyle := TFmxFormBorderStyle.bsNone;

  ResizeForm();
  FForm.Visible := True;
  Winapi.Windows.SetParent(FmxHandleToHWND(FForm.Handle), Handle);
end;

end.

No comments:

Post a Comment