Delphi fmx中在Form1里边嵌套一个Form2(fmx获取控件句柄)

把控件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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值