如何创建从其他几个组件继承的Delphi组件?

2024-01-06

我发现有关如何创建 delphi 组件的教程很好,但它们只使用现有组件之一作为继承操作的对象。像这样的东西

unit CountBtn;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 StdCtrls, ExtCtrls;

type
 TCountBtn = class(TButton)
  private
  FCount: integer;
  protected
  procedure Click;override;
  public
  procedure ShowCount;
  published
  property Count:integer read FCount write FCount;
  constructor Create(aowner:Tcomponent); override;
 end;

procedure Register;

implementation

procedure Register;
begin
 RegisterComponents('Mihan Components', [TCountBtn]);
end;

constructor TCountBtn.Create(aowner:Tcomponent);
begin
 inherited create(Aowner);
end;

procedure Tcountbtn.Click;
begin
 inherited click;
 FCount:=FCount+1;
end;

procedure TCountBtn.ShowCount;
begin
 Showmessage('On button '+ caption+' you clicked: '+inttostr(FCount)+' times');
end;

end.

但是如果我需要使用很少元素的组件该怎么办?可以说,我得到了Button and Edit场地。在按钮上单击编辑字段中应该显示与按钮上相同的文本。我开始这样做,但似乎它不会像我想要的那样工作:

unit TestComp;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TUiCompU = class(TCustomControl)
  private
    { Private declarations }
    FButton: TButton;
    FEdit: TEdit;

  protected
    { Protected declarations }
    procedure Paint; override;
    //wrong!
    procedure FButton.Click;override
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
  published
    { Published declarations }
    //wrong!
     property ButtonText: String read FButton.Caption write FButton.Caption;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Ui', [TUiCompU]);
end;

{ TUiCompU }

constructor TUiCompU.Create(AOwner: TComponent);
begin
  inherited;
  Width := 200;
  Height := 50;

  FButton := TButton.Create(Self);
  FButton.SetSubComponent(True);
  FButton.Parent := Self;
  FButton.Top := 8;
  FButton.Left := 50;
  FButton.Width := 35;
  FButton.Name := 'Button';

  FEdit := TEdit.Create(Self);
  FEdit.SetSubComponent(True);
  FEdit.Parent := Self;
  FEdit.Top := 8;
  FEdit.Left := 84;
  FEdit.Width := 121;
  FEdit.Name := 'Edit';
end;

procedure TUiCompU.Paint;
begin
  Canvas.Rectangle(ClientRect);
end;

end.

这里应该如何添加Click程序中,点击按钮的具体步骤是什么?是否有关于如何使用其他组件制作良好组件的良好教程? (顺便说一句,我需要创建类似幻灯片组件的东西)。 谢谢你,对不起我的英语。


您可以为子组件事件编写方法,但它有一个很大的弱点;如果您发布这些子组件,则存在有人会通过编写自己的方法窃取您此绑定的风险:

type
  TUiCompU = class(TCustomControl)
  private
    FEdit: TEdit;
    FButton: TButton;
    procedure ButtonClick(Sender: TObject);
    procedure EditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  public
    constructor Create(AOwner: TComponent); override;
  end;

implementation

constructor TUiCompU.Create(AOwner: TComponent);
begin
  inherited;

  FButton := TButton.Create(Self);
  ...
  FButton.OnClick := ButtonClick;

  FEdit := TEdit.Create(Self);
  ...
  FEdit.OnKeyDown := EditKeyDown;
end;

procedure TUiCompU.ButtonClick(Sender: TObject);
begin
  // do whatever you want here
end;

procedure TUiCompU.EditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  // do whatever you want here
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何创建从其他几个组件继承的Delphi组件? 的相关文章

随机推荐