如何创建继承自少数其他组件的Delphi组件?

 ddddd6192010_808 发布于 2023-02-08 17:31

我发现有关如何创建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.

但是,如果我需要使用少量元素的组件,我该怎么办?让我们说,我得到了ButtonEdit田地.然后在按钮上单击编辑字段中的文本应与按钮上的文本相同.我开始像这样做,但似乎它不会像我想的那样工作:

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程序,这是点击按钮的实际情况?是否有关于如何使用其他组件制作好组件的良好教程?(我需要创建类似幻灯片组件btw的东西).谢谢你,对不起我的英语.

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

    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;
    

    2023-02-08 17:34 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有