初期値の変更
グリッドのカスタマイズ:インデックス への移動
カレンダーは基本的に固定数の行と列からなるグリッドですが、すべての行に常に日付が入っているとは限りません。このため、グリッド プロパティ ColCount と RowCount は公開されておらず、カレンダーのユーザーが、1 週につき 7 日以外を表示しようとする可能性はほとんどないでしょう。それでも、それらのプロパティの初期値は設定して、週が常に7日間となるようにしなければなりません。
コンポーネントのプロパティの初期値を変更するには、希望する値を設定するためにコンストラクタをオーバーライドしなければなりません。コンストラクタは、virtual である必要があります。
コンストラクタをコンポーネントのオブジェクト宣言の public 部に追加し、その後、コンポーネントのユニットの implementation 部でその新しいコンストラクタを記述する必要があります。新しいコンストラクタ内の最初の文では、必ず継承コンストラクタを呼び出します。その後、StdCtrls ユニットを uses 句に追加します。
type
TSampleCalendar = class(TCustomGrid)
public
constructor Create(AOwner: TComponent); override;
// …
end;
// …
constructor TSampleCalendar.Create(AOwner: TComponent);
begin
inherited Create(AOwner); { call inherited constructor }
ColCount := 7; { always seven days/week }
RowCount := 7; { always six weeks plus the headings }
FixedCols := 0; { no row labels }
FixedRows := 1; { one row for day names }
ScrollBars := ssNone; { no need to scroll }
Options := Options - [goRangeSelect] + [goDrawFocusSelected]; {disable range selection}
end;
//header file
class PACKAGE TSampleCalendar : public TCustomGrid
{
protected:
virtual void __fastcall DrawCell(int ACol, int ARow, const Windows::TRect &Rect,
TGridDrawState AState);
// …
public:
__fastcall TSampleCalendar(TComponent *Owner); // the added constructor
// …
};
//implementation file
__fastcall TSampleCalendar::TSampleCalendar(TComponent *Owner) : TCustomGrid(Owner)
{
ColCount = 7;
RowCount = 7;
FixedCols = 0;
FixedRows = 1;
ScrollBars = ssNone;
Options = (Options >> goRangeSelect) << goDrawFocusSelected;
}
void __fastcall TSampleCalendar::DrawCell(int ACol, int ARow, const Windows::TRect
&ARect, TGridDrawState AState)
{
}
これで、7列7行となり、最初の行は固定つまり非スクロールのカレンダーとなりました。