把控件TPanel属性 ControlType 设置为 Platform ,直接调用
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,
FMX.Dialogs, FMX.TreeView, FMX.ListBox, FMX.Layouts, FMX.Objects,
FMX.StdCtrls, FMX.Controls.Presentation;
type
TForm1 = class(TForm)
pnl1: TPanel;
rect1: TRectangle;
btn1: TButton;
procedure FormResize(Sender: TObject);
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
Unit2, Winapi.Windows, FMX.Platform.Win, FMX.Presentation.Win.Style;
{$R *.fmx}
function FmxObjectToWnd(FmxControl: TPresentedControl): NativeUInt;
var
Idx: Integer;
begin
Result := 0;
if FmxControl.ControlType = TControlType.platform then
for Idx := 0 to FmxControl.ChildrenCount - 1 do
begin
if FmxControl.Children[Idx] is TNativeScene then
begin
Exit(TNativeScene(FmxControl.Children[Idx]).Handle.Wnd);
end;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
//Showmessage(IntToStr( FmxObjectToWnd(pnl1)));
Form2 := TForm2.Create(pnl1);
Form2.BorderStyle := TFmxFormBorderStyle.bsNone;
Form2.Show;
//关键这两行代码
Winapi.Windows.SetParent(WindowHandleToPlatform(Form2.Handle).Wnd, FmxObjectToWnd(pnl1));
MoveWindow(WindowHandleToPlatform(Form2.Handle).Wnd, 0, 0, Round(pnl1.Width), Round(pnl1.Height), True);
end;
procedure TForm1.FormResize(Sender: TObject);
begin
if Form2 <> nil then
MoveWindow(WindowHandleToPlatform(Form2.Handle).Wnd, 0, 0, Round(pnl1.Width), Round(pnl1.Height), True);
end;
end.