Example 52 : custom drawing and custom drawing printing support

TAdvStringGrid has like TStringGrid always supported the OnDrawCell event to allow custom cell drawing. The OnDrawCell event is implemented in TStringGrid and the major drawback of this event is that it does provide the Canvas as a parameter. This makes it troublesome to use the event to allow custom drawing on the display canvas as well as on the printer canvas. Another problem with the OnDrawCell event is that there is no way to let the grid know about the size required to paint the custom cell. Therefore, TAdvStringGrid implements 2 new events that allow easy custom drawing on display as well as on printer through the events : OnCustomCellDraw and OnCustomCellSize.

TAdvStringGrid custom draw on screen:
vcl grid custom drawing

TAdvStringGrid custom draw on printer:
vcl grid custom drawing

The OnCustomCellDraw is similer to the OnDrawCell event but provides two necessary extra parameters : Canvas: TCanvas and Printing: Boolean;

For cell display on screen, the Canvas property is equal to the grid control canvas, while during printing this canvas is equal to the printer canvas. The Printing boolean parameter indicates whether custom drawing is done during the grid on screen display or during a printout. With this extra parameter it could for example be possible to provide grey scale drawing during printing and full color for on screen display.

This is the code in the OnCustomCellDraw event of the sample application:

procedure TForm1.AdvStringGrid1CustomCellDraw(Sender: TObject;
  Canvas: TCanvas; ACol, ARow: Integer; AState: TGridDrawState;
  ARect: TRect; Printing: Boolean);
  if (arow>0) and (acol>0) then
    Canvas.Pen.Color := clBlack;

    case (arow + acol + 2) mod 3 of
    0: Canvas.Brush.Color := clRed;
    1: Canvas.Brush.Color := clBlue;
    2: Canvas.Brush.Color := clGreen;

    case (arow + acol) mod 3 of
        Canvas.Polygon([Point(ARect.Left + (ARect.Right-ARect.Left) shr 1,ARect.Top),Point(ARect.Left,ARect.Bottom),Point(ARect.Right,ARect.Bottom)]);

    Canvas.Font.Size := 12;
    Canvas.TextOut(ARect.Left + (ARect.Right-ARect.Left) shr 1,ARect.Top + (ARect.Bottom - ARect.Top) shr 1,IntToStr(ACol));

As TAdvStringGrid normally performs automatic sizing of columns and rows, it is necessary to provide an event OnCustomCellSize to specify the size required for a custom drawn cell as such cells normally do not have text with which automatic cell size calculation is done. The OnCustomCellSize event simply queries the requested X and Y size for a cell (in logical point sizes) through the ASize parameter:

procedure TForm1.AdvStringGrid1CustomCellSize(Sender: TObject;
  Canvas: TCanvas; ACol, ARow: Integer; var ASize: TPoint;
  Printing: Boolean);
  ASize := Point(100,100);

With these 2 extra events, an entirely new range of capabilities for grid customization on screen as well as on printouts are opened.

Delphi project & source files for downloading included in the main demos distribution for Delphi.