微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在将一个大型项目转换为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;

解决方法

问题是控件没有为其注册的样式.因此,自然的解决方案就是为您做到这一点.

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

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

function TButtonBase.GetDefaultStyleLookupName: string;
begin
  Result := 'Buttonstyle';
end;

现在从TButtonBase派生你的类.

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

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

原文地址:https://www.jb51.cc/delphi/102260.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐