RAD Studio |  |

Filling in the Cells

A grid control fills in its contents cell-by-cell. In the case of the calendar, that means calculating which date, if any, belongs in each cell. The default drawing for grid cells takes place in a virtual method called DrawCell.

To fill in the contents of grid cells, override the DrawCell method.

The easiest part to fill in is the heading cells in the fixed row. The runtime library contains an array with short day names, so for the calendar, use the appropriate one for each column:

Copy Code

    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;

Copy Code

    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);
    }

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!