In fact, answering this question, the explicit question about rossmcm in his commentary on the code of Andreas Reybrend, it also complements the answer to the original question.
Drawing an image outside the grid, but still within the bounds of the StringGrid control, you can achieve the following:
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;
If you also want to draw an image in the cells, combine both methods. The fact that they do not adhere to the same approach, since Andreas uses events where I declare a descendant, should not lead to great difficulties in merging.
NGLN source share