Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建继承自少数其他组件的Delphi组件?_Delphi_Components - Fatal编程技术网

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

如何创建继承自少数其他组件的Delphi组件?,delphi,components,Delphi,Components,我找到的关于如何创建delphi组件的教程很不错,但它们只使用一个现有组件作为对象来继承操作。像这样的 unit CountBtn; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TCountBtn = class(TButton) private FCount: integer; prote

我找到的关于如何创建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.
但如果我需要使用很少元素的组件,我应该怎么做呢?比方说,我得到了
按钮
编辑
字段。在编辑字段中单击按钮时,显示的文本应与按钮上的文本相同。我开始这样做,但似乎不会像我想的那样工作:

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.
我应该如何在此处添加
单击
过程,单击按钮才是真正的过程?是否有关于如何使用其他组件制作好组件的好教程?(顺便说一句,我需要创建类似幻灯片组件的东西)。
谢谢你,对不起我的英语。

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

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;

哦,谢谢你的回答!我甚至在几分钟前看到了你的一个答案!但是有没有办法避免这种绑定呢?好吧,这种绑定是实现您要实现的最简单的方法,除了窃取事件处理程序的风险(如果您将这些子控件公开),它没有什么错。据我所知,任何其他方法都需要破解这些分包,例如,通过覆盖其窗口过程或从其动态方法中引入某种通知机制(如
单击
按键
等方法)。尝试海关集装箱和组件包。它是由Borland莫斯科办事处为Delphi 4/5创建的,用于将表单(所有组件都放在那里、事件、属性等)转换为全新的复合组件。这很酷,我认为这种方法(基于封装)比Delphi5引入的
TFrame
更好(而且没有封装)。我听说在SourceForge上有一个到Delphi XE2的CCCP端口,但我没有亲自尝试过。还有一些与本主题相关的高级知识:@user928177263很高兴看到这个问题很有用,ppl正在检查它