既然你想正確完成標題屬性,梅森的答案不會工作,因爲他錯過了'csSetCaption'的事情,他的'默認'的建議不會工作,因爲標題和你的屬性都是字符串類型。
下面是你想要的單位。
的行爲如下:
- intially Caption屬性的值將是「評論」
- 用戶可以覆蓋在設計時通過設置新的值
(如果你不想要2.,那麼你需要像Ken提到的那樣用一個被覆蓋的Loaded方法來指定Caption屬性;但是,從你的問題中不清楚你是否需要這個方法,如果你願意的話,請重新說明你的問題。
這是代碼的工作原理。
對於字符串屬性,您不能提示任何默認的流式系統。 但是,您可以在構造函數中爲設計時間設置初始值:Caption:= DefaultCustomSpeedButtonCaption;
對於Caption屬性,您還必須禁用Caption屬性的默認分配(否則您的組件將自動獲得像'CustomSpeedButton1'這樣的標題)。 這一行代碼適用於您:ControlStyle:= ControlStyle - [csSetCaption];
最後,將組件註冊拆分爲單獨的單元是一種很好的做法。 這使您可以擁有一個設計時包,它可以在IDE中註冊組件,並提供運行時包(或根本沒有包),以便在應用程序中使用組件。
如果您有一個組件圖標,那麼您也將其加載到註冊單元中(因爲它只在設計時需要)。
雷Konopka的寫了書面成分一個很好的書,仍然是非常有效的:Developing Custom Delphi 3 Components 和很多優秀的德爾福書,它是絕版的,但你可以order a PDF copy on his site。
我不確定CommentTitle和CommentText屬性是什麼,所以我將它們放在下面的代碼中。
清單1:實際的組件
unit CustomSpeedButtonUnit;
interface
uses
SysUtils, Classes, Controls, Buttons;
const
DefaultCustomSpeedButtonCaption = 'Comments';
type
TCustomCustomSpeedButton = class(TSpeedButton)
strict private
FCommentText: string;
FCommentTitle: string;
strict protected
procedure SetCommentText(const Value: string); virtual;
procedure SetCommentTitle(const Value: string); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property CommentTitle: string read FCommentTitle write SetCommentTitle;
property CommentText: string read FCommentText write SetCommentText;
end;
TCustomSpeedButton = class(TCustomCustomSpeedButton)
published
// note you cannot use 'default' for string types; 'default' is only valid for ordinal ordinal, pointer or small set type
// [DCC Error] CustomSpeedButtonUnit.pas(29): E2146 Default values must be of ordinal, pointer or small set type
// property Caption default DefaultCustomSpeedButtonCaption;
property CommentTitle;
property CommentText;
end;
implementation
constructor TCustomCustomSpeedButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Caption := DefaultCustomSpeedButtonCaption;
ControlStyle := ControlStyle - [csSetCaption];
end;
destructor TCustomCustomSpeedButton.Destroy;
begin
inherited Destroy;
end;
procedure TCustomCustomSpeedButton.SetCommentText(const Value: string);
begin
FCommentText := Value;
end;
procedure TCustomCustomSpeedButton.SetCommentTitle(const Value: string);
begin
FCommentTitle := Value;
end;
end.
清單2:組件註冊
unit CustomSpeedButtonRegistrationUnit;
interface
procedure Register;
implementation
uses
CustomSpeedButtonUnit;
procedure Register;
begin
RegisterComponents('Standard', [TCustomSpeedButton]);
end;
end.
你的意思是你想要的*默認*標題爲「評論」,或者你的意思是你想要的標題爲*總*是「評論,」不管是什麼開發人員將其更改爲以後? – 2009-08-27 20:35:38
默認標題爲'評論' – Rafe 2009-08-27 20:40:09