从TButton继承的自定义按钮不显示

Han*_*ans 8 delphi firemonkey delphi-xe6

我正在将一个大型项目转换为Firemonkey,并且我们有一些自定义按钮,这些按钮不会显示在表单上.我已将问题分离到一个非常简单的项目:

使用下面的代码,在移动和桌面上(使用Delphi XE6中的默认新应用程序),创建tTestButton1工作正常,但tTestButton2不会在窗体上显示.怎么可能?

type
tTestButton1 = class(TButton);
tTestButton2 = class(tTestButton1);

tMainForm = class(TForm)
private
  fTestButton: TButton;
public
  constructor Create(aOwner: TComponent); override;
end;

constructor tMainForm .Create(aOwner: TComponent);
begin
  inherited;

//  fTestButton := tTestButton1.Create(Self); // this works fine (used instead of next line)
  fTestButton := tTestButton2.Create(Self);  //this button does not show up
  fTestButton.Text := 'Test';
  fTestButton.Parent := Self;
  fTestButton.Visible := True;
  fTestButton.Position.X := 20;
  fTestButton.Position.Y := 20;
end;
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 4

问题是该控件没有为其注册样式。所以自然的解决方案就是你这样做。

但这是一个合理的工作量,我希望您真正想做的就是安排该控件使用与TButton. 像这样实现:

type
  TButtonBase = class(TButton)
  protected
    function GetDefaultStyleLookupName: string; override;
  end;

function TButtonBase.GetDefaultStyleLookupName: string;
begin
  Result := 'Buttonstyle';
end;
Run Code Online (Sandbox Code Playgroud)

现在从 派生您的类TButtonBase

type
  tTestButton1 = class(TButtonBase);
  tTestButton2 = class(tTestButton1);
Run Code Online (Sandbox Code Playgroud)

TButtonBase派生的类将使用名为 的样式,而不是根据控件的类名查找样式Buttonstyle