|
fm被装载时,Delphi查找组件名以恢复对象引用的值。如果你必须要在一个组件内部保存一个完整的组件,则你必须实现对内部组件的属性的访问委托。
确认属性的类继承自TPersistent而来,并且该类覆盖了Assign方法。通过调用Assign来实现属性的写方法。(TPersistent,在Classes单元中定义,并不是必须的,但确是一个最简单的方法——复制一个对象。否则,你将花费两倍的代价在任何其他用到的类中书写Assigned方法。)读方法可以提供对字段的直接访问。如果该对象有一个OnChange的事件,你最好将设置其值以了解对象何时作了改变。例子 2-12显示了一个典型的使用对象属性的方法。例子中定义了一个图形控件,用于在需要时在其范围内以平铺的方式显示位图。属性Bitmap存放了一个TBitmap对象。
例 2-12:声明和使用对象类型的属性 unit Tile; interface uses SysUtils, Classes, Controls, Graphics; type // Tile a bitmap TTile = class(TGraphicControl) private fBitmap: TBitmap; procedure SetBitmap(NewBitmap: TBitmap); procedure BitmapChanged(Sender: TObject); protected procedure Paint; override; public constructor Create(Owner: TComponent); override; destructor Destroy; override; published property Align; property Bitmap: TBitmap read fBitmap write SetBitmap; property OnClick; property OnDblClick; //还有许多有用的方法,限于空间不一一列出。详见TControl。 end; implementation { TTile } // Create the bitmap when creating the control. constructor TTile.Create(Owner: TComponent); begin inherited; fBitmap := TBitmap.Create; fBitmap.OnChange := BitmapChanged; end; // Free the bitmap when destroying the control. destructor TTile.Destroy; begin FreeAndNil(fBitmap); inherited; end; // When the bitmap changes, redraw the control. procedure TTile.BitmapChanged(Sender: TObject); begin Invalidate; end; // Paint the control by tiling the bitmap. If there is no // bitmap, don''''t paint anything. procedure TTile.Paint; var X, Y: Integer; begin if (Bitmap.Width = 0) or (Bitmap.Height = 0) then Exit; Y := 0; while Y < ClientHeight do begin X := 0; while X < ClientWidth do begin Canvas.Draw(X, Y, Bitmap); Inc(X, Bitmap.Width); end; Inc(Y, Bitmap.Height); end; end; //通过复制TBitmap对象的方式设置新值 procedure TTile.SetBitmap(NewBitmap: TBitmap); begin fBitmap.Assign(NewBitmap); end; end. PartI PartII 上一页 [1] [2] [3] 下一页 [VB.NET程序]VB.Net中文教程(13) Whole-Part关系 [Delphi程序]The Delphi Object Model (PART III) [Delphi程序]The Delphi Object Model (PART II) [Delphi程序]The Delphi Object Model (PART I) [Delphi程序]Delphi对象模型(Part III) [Delphi程序]Delphi对象模型(Part II) [Delphi程序]Delphi对象模型(Part I) [Delphi程序]Delphi对象模型(Part IV) [Delphi程序]Delphi对象模型(Part VI) [Delphi程序]防止全局hook入侵Delphi版,2000以上系统适用(pa…
|