6

I do custom drawing of a Delphi TStringGrid using the OnDrawCell event. There is no problem with the area covered by cells, but how do I paint the background right of the rightmost column and below the last row ?

(Edit) Painting is not really necessary, I just want to set the color used for background. I am using XE2 and investigating VCL styles. Even in default drawing, setting Colors in a stringgrid, seams to have no effect at all.

TIA

TheRoadrunner
  • 1,281
  • 14
  • 34
  • 2
    OK I finally found it. The problem was the DrawingStyle property of TStringGrid that defaults to gdsThemed. Setting it to gdsClassic makes the grids Color property kick in - also for the background. Issue solved. Thanks to Andreas for a way to completely control the background painting process, but it's an overkill for my issue. rgds TheRoadrunner – TheRoadrunner Dec 09 '11 at 07:27

1 Answers1

2

This is some code I found with google (It is not from me, I could not find the name of the author, maybe it comes from StackExchange on some way...). It defines a descendant from TStringGrid and implements a new background drawing. (The example uses a bitmap, but you easily can change that...)

  type
  TStringGrid = class(Grids.TStringGrid)
  private
    FGraphic: TGraphic;
    FStretched: Boolean;
    function BackgroundVisible(var ClipRect: TRect): Boolean;
    procedure PaintBackground;
  protected
    procedure Paint; override;
    procedure Resize; override;
    procedure TopLeftChanged; override;
  public
    property BackgroundGraphic: TGraphic read FGraphic write FGraphic;
    property BackgroundStretched: Boolean read FStretched write FStretched;
  end;

  TForm1 = class(TForm)
    StringGrid: TStringGrid;
    Image: TImage;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TStringGrid }

function TStringGrid.BackgroundVisible(var ClipRect: TRect): Boolean;
var
  Info: TGridDrawInfo;
  R: TRect;
begin
  CalcDrawInfo(Info);
  SetRect(ClipRect, 0, 0, Info.Horz.GridBoundary, Info.Vert.GridBoundary);
  R := ClientRect;
  Result := (ClipRect.Right < R.Right) or (ClipRect.Bottom < R.Bottom);
end;

procedure TStringGrid.Paint;
begin
  inherited Paint;
  PaintBackground;
end;

procedure TStringGrid.PaintBackground;
var
  R: TRect;
begin
  if (FGraphic <> nil) and BackgroundVisible(R) then
  begin
    with R do
      ExcludeClipRect(Canvas.Handle, Left, Top, Right, Bottom);
    if FStretched then
      Canvas.StretchDraw(ClientRect, FGraphic)
    else
      Canvas.Draw(0, 0, FGraphic);
  end;
end;

procedure TStringGrid.Resize;
begin
  inherited Resize;
  PaintBackground;
end;

procedure TStringGrid.TopLeftChanged;
begin
  inherited TopLeftChanged;
  PaintBackground;
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Usage: 
  StringGrid.BackgroundGraphic := Image.Picture.Graphic;
  StringGrid.BackgroundStretched := True;
end;
Andreas
  • 1,334
  • 1
  • 10
  • 21
  • 2
    Its from an answer by NGLN here on SO. [delphi-stringgrid-with-picture-in-background](http://stackoverflow.com/questions/5285659/delphi-stringgrid-with-picture-in-background). It's supposed to be used with a picture in the background. – LU RD Dec 09 '11 at 07:24
  • OK, thank you! It sould be no problem to use the code to draw other backgrounds onto the grid. – Andreas Dec 09 '11 at 07:27
  • 2
    No problem at all, but to answer the question you can elaborate a little more how to draw a background color. – LU RD Dec 09 '11 at 07:31
  • 2
    @Andreas And you could have added the link yourself. It is customary to attribute information you re-iterate to its source. – Marjan Venema Dec 09 '11 at 08:09
  • @LU RD: The question was edited. In the beginning there was no hint that it is only the color of the background to be painted. – Andreas Dec 09 '11 at 11:13
  • @Marjan: Yes, of course you are right. I did not find the link in SO by myself. Sorry. – Andreas Dec 09 '11 at 11:14
  • @Andreas: :-) I tend to keep bookmarks of stuff I used. With Google Chrome it is very easy to search through them... and I only get hit with stuff I bookmarked (instead of everything else on the web). – Marjan Venema Dec 09 '11 at 11:45