How do I paint the background of TStringGrid
Asked Answered
D

1

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

Delphiadelphic answered 9/12, 2011 at 6:46 Comment(1)
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 TheRoadrunnerDelphiadelphic
T
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;
Transponder answered 9/12, 2011 at 7:15 Comment(7)
Its from an answer by NGLN here on SO. delphi-stringgrid-with-picture-in-background. It's supposed to be used with a picture in the background.Claudclauddetta
OK, thank you! It sould be no problem to use the code to draw other backgrounds onto the grid.Transponder
No problem at all, but to answer the question you can elaborate a little more how to draw a background color.Claudclauddetta
@Transponder And you could have added the link yourself. It is customary to attribute information you re-iterate to its source.Stat
@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.Transponder
@Marjan: Yes, of course you are right. I did not find the link in SO by myself. Sorry.Transponder
@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).Stat

© 2022 - 2024 — McMap. All rights reserved.