線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2166
推到 Plurk!
推到 Facebook!

請問如何在自製元件內控制Form上的元件

答題得分者是:jow
chenyk
高階會員


發表:14
回覆:95
積分:171
註冊:2002-07-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-10-17 17:49:34 IP:202.39.xxx.xxx 訂閱
我有一個自製的元件Txx,是繼承TComponent 來的。
然後我在視窗上放了一個進度表元件 Tgauge,
希望由 Tgauge 來顯示 Txx 處理資料的進度,
請問要怎麼做才能將兩個物件關聯起來?

謝謝
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-10-17 20:53:22 IP:123.193.xxx.xxx 訂閱
程式碼謹供參考   ^o^

[code delphi]
unit Unit1;

interface

uses
Forms, Gauges, Classes, Controls, ExtCtrls, StdCtrls;

type
//(0)因為Txx的型態宣告在 TForm1 之後, 若要在 TForm1 中引用 Txx class,
//則必須對 Txx 作預先宣告.

Txx = class; //pre-declaration

//(1) TForm1 與 TForm 的關係
//TForm1 class 從 TForm class 繼承, 改寫的部分是在
//TForm1 class 上多了一個, TGauge 物件

//(2) TGauge 與 TForm 的關係
//TGauge 在設計期間被擺在 TForm1上, 所以 TGauge的 Owner 與 Parent
//都是 TForm1; Owner的權責在於物件生命週期結束時, 要負責物件所佔
//記憶體的釋放; Parent的ControlStyle具備有 csAcceptsControls 的特性,
//主要是用來承載可視元件的顯示.

TForm1 = class(TForm)
Gauge1: TGauge;
Gauge2: TGauge;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
xx: Txx;
procedure DO_PROGRESS(Sender: TObject; Progress: Integer);
public
end;

//(3)Form1, TForm1 與 Designer 的關係.
//以下的宣告 Form1: TForm1 宣告了一個型態為 TForm1 的物件變數,
//在執行期間 Designer 會自動產生一個 TForm1 的實際物件, 並將
//所配置記憶體的位址傳回, 指定給 Form1 變數.

//(4) 宣告 Txx, 繼承自 TComponent.

//定義事件型態, 通知 Txx 元件外部, 有某特定型態的事件發生, 由外部指定
//事件處理程序.
TProgressEvent = procedure(Sender: TObject; Progress: Integer) of object;

Txx = class(TComponent)
private
FOnProgress: TProgressEvent;
procedure SetProgress(const Value: Integer);
function GetActive: Boolean;
procedure SetActive(const Value: Boolean);
protected
Timer: TTimer;
FProgress: Integer; //Keep Progress
procedure DO_TIMER(Sender: TObject);//Event Handler
public
constructor Create(AOwner: TComponent); override;
property Progress: Integer read FProgress write SetProgress;
property Active: Boolean read GetActive write SetActive;
published
property OnProgress: TProgressEvent read FOnProgress write FOnProgress;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ Txx }
constructor Txx.Create(AOwner: TComponent);
begin
inherited;
Timer := TTimer.Create(Self);//Txx為Owner, 負責釋放 Timer 物件.
Timer.OnTimer := DO_TIMER; //指定 OnTimer event handler.
Timer.Interval := 100;
Timer.Enabled := False;

//Initial 型態為 TForm1 的 Owner 上的 Gauge1.
if Owner is TForm1 then
begin
TForm1(Owner).Gauge1.MaxValue := 100;
TForm1(Owner).Gauge1.MinValue := 0;
end;

end;

procedure Txx.DO_TIMER(Sender: TObject);
begin
Progress := (Progress 1) mod 100;
end;

function Txx.GetActive: Boolean;
begin
Result := Timer.Enabled;
end;

procedure Txx.SetActive(const Value: Boolean);
begin
Timer.Enabled := Value;
if Timer.Enabled then
Progress := 0;
end;

procedure Txx.SetProgress(const Value: Integer);
begin
if Value <> FProgress then
begin
FProgress := Value;

//(A)直接驅動 TForm1 上的 Gauge1.
if Owner is TForm1 then
TForm1(Owner).Gauge1.Progress := FProgress;

//(B)以事件方式通知Txx元件外部, 由外部物件指定 Event Handler
if Assigned(FOnProgress) then
FOnProgress(Self, FProgress);

end;
end;

//------------------------------------------------------------------------------
{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
Gauge2.MaxValue := 100;
Gauge2.MinValue := 0;
Gauge2.Progress := 0;
xx := Txx.Create(Self);
xx.OnProgress := Self.DO_PROGRESS;//指定event handler
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if xx.Active then xx.Active := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(xx) then
begin
xx.Active := not xx.Active;
if xx.Active then Button1.Caption := '停止'
else Button1.Caption := '啟動';
end;
end;

//TForm1指定的 Txx.OnProgesss 的事件處理程序...
procedure TForm1.DO_PROGRESS(Sender: TObject; Progress: Integer);
begin
Gauge2.Progress := Progress;
end;

end.

[/code]

測試碼下載:
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=90857
編輯記錄
jow 重新編輯於 2007-10-17 21:00:04, 註解 無‧
chenyk
高階會員


發表:14
回覆:95
積分:171
註冊:2002-07-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-10-18 22:07:31 IP:125.231.xxx.xxx 訂閱
真是太棒啦
讓小弟獲益匪淺

謝謝 jaw 大的答覆。
系統時間:2024-04-29 9:01:07
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!