如何创建从其他几个组件继承的Delphi组件? [英] How to create Delphi component inherited from few other components?

查看:94
本文介绍了如何创建从其他几个组件继承的Delphi组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有关如何创建delphi组件的教程很好,但是它们仅使用现有组件之一作为对象来继承操作。

Tutorials that I found about how to create delphi components were nice, but they only used one of existing components as object to inherit actions from. Something like this

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 Edit 字段。在按钮上单击时,在编辑字段中应显示与按钮上相同的文本。我开始这样做,但似乎无法按我的意愿工作:

But what should I do if I need component which use few elements? Lets say, I got Button and Edit field. And on button click there in edit field should appers text the same as on button. I start to make it like this, but seems like it's not gonna work as I want:

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.

如何在此处添加点击过程,单击哪个按钮可以实现?并且有关于如何使用其他组件制作好的组件的良好教程吗? (我需要创建类似幻灯片组件btw的东西)。
谢谢,对不起我的英语。

How should I add here Click procedure, which is realte to click on the button? And is there are good tutorial about how to made good components using others? (I need to create something like slideshow component btw). Thank you, and sorry for my english.

推荐答案

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

You can write methods for the subcomponent events, but it has one big weakness; if you publish those subcomponents, there is a risk that someone will steal you this binding by writing own method:

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;

这篇关于如何创建从其他几个组件继承的Delphi组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆