教程,我發現如何創建delphi組件很好,但他們只使用現有的組件之一作爲對象來繼承動作。像這樣的東西如何創建從少數其他組件繼承的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
和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
程序,這是realte點擊按鈕?有沒有關於如何使用他人制作好組件的好教程? (我需要創建像幻燈片組件btw)。 謝謝,對不起我的英文。
嘗試海關容器和組件包。它由Borland莫斯科辦事處爲Delphi 4/5創建,將表單(將所有組件放置在那裏,事件,屬性等)變成一個全新的複合組件。這很酷,我認爲這種方法(基於封裝)比Delphi 5引入的「TFrame」更好(並且沒有封裝)。我在SourceForge上聽說有一個用於Delphi XE2的CCCP端口,但我沒有親自嘗試過。舊的如何去除圖片:http://www.howtodothings.com/computers/a1167-custom-containers-pack-ccpack-5.html –
一些高級的東西與這個主題相關:https:// stackoverflow。 com/questions/582903/whats-the-difference-between-createwnd-and-createwindowhandle – Ampere
@ user928177263很高興看到這個問題很有用,並且ppl檢查它 – DanilGholtsman