2013-12-21 65 views
4

教程,我發現如何創建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. 

但是,如果我需要使用少量元素的組件,該怎麼辦?可以說,我有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程序,這是realte點擊按鈕?有沒有關於如何使用他人制作好組件的好教程? (我需要創建像幻燈片組件btw)。 謝謝,對不起我的英文。

+1

嘗試海關容器和組件包。它由Borland莫斯科辦事處爲Delphi 4/5創建,將表單(將所有組件放置在那裏,事件,屬性等)變成一個全新的複合組件。這很酷,我認爲這種方法(基於封裝)比Delphi 5引入的「TFrame」更好(並且沒有封裝)。我在SourceForge上聽說有一個用於Delphi XE2的CCCP端口,但我沒有親自嘗試過。舊的如何去除圖片:http://www.howtodothings.com/computers/a1167-custom-containers-pack-ccpack-5.html –

+1

一些高級的東西與這個主題相關:https:// stackoverflow。 com/questions/582903/whats-the-difference-between-createwnd-and-createwindowhandle – Ampere

+0

@ user928177263很高興看到這個問題很有用,並且ppl檢查它 – DanilGholtsman

回答

6

您可以編寫子組件事件的方法,但它有一個很大的弱點;如果您發佈這些子組件,則有可能會有人通過自己的方法來竊取您的綁定:

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; 
+0

哦謝謝你的回答!我幾乎在幾分鐘前甚至看到了你的答案!但有沒有辦法避免這種綁定? – DanilGholtsman

+3

那麼,綁定是實現你要實現的最簡單的方法,除了竊取事件處理程序的風險(如果你公開這些子控件),它沒有任何錯誤。據我所知,任何其他方式都需要破解這些子控制,例如通過覆蓋它們的窗口過程或從它們的動態方法(像Click,KeyDown等方法)引入某種通知機制。 – TLama