圖形元件外觀由屬性組合決定。為 TSampleShape 增加 Shape 屬性支援六種幾何形狀:
type
TSampleShapeType = (sstRectangle, sstSquare, sstRoundRect,
sstRoundSquare, sstEllipse, sstCircle);
TSampleShape = class(TGraphicControl)
end;
枚舉提供設計期下拉選單、型別安全與程式碼可讀性。
type
TSampleShape = class(TGraphicControl)
private
FShape: TSampleShapeType;
procedure SetShape(Value: TSampleShapeType);
published
property Shape: TSampleShapeType read FShape write SetShape;
end;
procedure TSampleShape.SetShape(Value: TSampleShapeType);
begin
if FShape <> Value then
begin
FShape := Value;
Invalidate(True); // 標記客戶區失效,觸發 Paint
end;
end;
守衛條件避免無意義重繪;Invalidate(True) 擦除背景並排程重繪。
constructor Create(AOwner: TComponent); override; / destructor Destroy; override;property Height default 65; property Width default 65; (設計期屬性檢視器顯示粗體預設值,DFM 不存儲預設值)。inherited Create → 設定 Width/Height → 初始化 Owned 物件。釋放 Owned 物件 → inherited Destroy。此模式為所有圖形/視覺元件開發的標準範本。