How to draw a TStringGrid background

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

(Edit) Painting is not really needed, I just want to set the color used for the background. I am using XE2 and researching VCL styles. Even when drawing by default, setting the colors in the stringgrid, the seams have no effect.

TIA

+6
source share
1 answer

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

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; 
+2
source

Source: https://habr.com/ru/post/903436/


All Articles