PRODUCTS

FEATURED PRODUCT

Grid, menus, calendars, advanced edits, navigational controls and much more... to create feature-rich IntraWeb applications faster

License only 125 EUR See More

SEARCH

LOGIN

Customer login to access products, support information & special benefits.

NEWS ALERTS

Add your e-mail address here to receive the monthly TMS Software alerts.

 

LINKS

EXAMPLE 52

TAdvStringGrid

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: TAdvStringGrid

TAdvStringGrid custom draw on printer: TAdvStringGrid

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);
begin
  if (arow>0) and (acol>0) then
  begin
    InflateRect(arect,-5,-5);
    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;
    end;

    case (arow + acol) mod 3 of
    0:begin
        Canvas.Ellipse(ARect);
       end;
    1:begin
        Canvas.Rectangle(ARect);
       end;
    2:begin
        Canvas.Polygon([Point(ARect.Left + (ARect.Right-ARect.Left) shr 1,ARect.Top),Point(ARect.Left,ARect.Bottom),Point(ARect.Right,ARect.Bottom)]);
       end;
    end;

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

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);
begin
  ASize := Point(100,100);
end;

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.

The project and source files have been written with Delphi 6,7. To use these files in other versions of Delphi, ignore any remarks when opening the form files and save the files. After this, compilation can be done. The error messages are due to properties included in the Delphi 6 form file, but not available in lower versions of Delphi.  

Copyright © 1995 - 2008 TMS Software