セルの内容を設定する

提供: RAD Studio
移動先: 案内検索

グリッドのカスタマイズ:インデックス への移動

グリッド コントロールは、セル 1 つ 1 つにコンテンツを入れていきます。カレンダーの場合、どの日付がどのセルに属するのかを計算していきます。グリッドのデフォルト描画は、仮想メソッド DrawCell で発生します。

グリッド セルのコンテンツを埋めるには、DrawCell メソッドをオーバーライドします。

埋めていく過程のもっとも用意な部分は、固定行のヘッダ セルです。ランタイム ライブラリには、曜日の略称の配列が含まれているので、カレンダーの場合、各列に適切なものを使用していきます:



type
  TSampleCalendar = class(TCustomGrid)
  protected
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
      override;
  end;
.
.
.
procedure TSampleCalendar.DrawCell(ACol, ARow: Longint; ARect: TRect;
  AState: TGridDrawState);
begin
  if ARow = 0 then
    Canvas.TextOut(ARect.Left, ARect.Top, ShortDayNames[ACol + 1]);    { use RTL strings }
end;



void __fastcall TSampleCalendar::DrawCell(int ACol, int ARow, const Windows::TRect &ARect,
  TGridDrawState AState)
{
  String TheText;
  int TempDay;
  if (ARow == 0) TheText = ShortDayNames[ACol + 1];
  else
  {
    TheText = "";
    TempDay = DayNum(ACol, ARow);                   // DayNum is defined later
    if (TempDay != -1) TheText = IntToStr(TempDay);
  }
  Canvas->TextRect(ARect, ARect.Left + (ARect.Right - ARect.Left
   - Canvas->TextWidth(TheText)) / 2,
  ARect.Top + (ARect.Bottom - ARect.Top - Canvas->TextHeight(TheText)) / 2, TheText);
}

トピック

関連項目