本文整理自网络,侵删。
Delphi fmx 获取控件句柄
Fmx 框架控件如果 从 TPresentedControl 类继承下来的,会有一个属性,ControlType,它是一个枚举。
打开 FMX.Controls 单元定义:TControlType = (Styled, Platform);
控件的ControlType属性默认为 Styled,如果使用 Platform ,则使用当前平台原生创建。
在 Win 平台原生创建是有句柄的。我通过遍历里面的对象,获取到它的 TWinNativeScene 类,取出句柄。
原码如下:
uses
FMX.Controls.Presentation, FMX.Presentation.Win.Style, FMX.Controls
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 TWinNativeScene then begin
Exit(TWinNativeScene(FmxControl.Children[Idx]).Handle.Wnd);
end;
end;
end;
调用方法:
把控件属性 ControlType 设置为 Platform ,直接调用
Showmessage( IntToStr( FmxObjectToWnd(Panel1) ) ) ;
来源:http://delphifmx.com/node/80
相关阅读 >>
Delphi 获取动态创建的image与scrollbox的相对位置
更多相关阅读请进入《Delphi》频道 >>