Delphi StringGrid with background image

Hi, does anyone know if it is possible to display an image as a background for a grid of rows, or is anyone aware of any free Grid component that can do this.

thanks

Colin

+3
source share
3 answers

You can use TDrawGrid (or TStringGrid ), which supports owner drawing, and do

 procedure TForm1.FormCreate(Sender: TObject); begin FBg := TBitmap.Create; FBg.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\Sample.bmp'); end; 

where FBg is TBitmap (e.g. in a form class) and then do

 procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var r: TRect; begin if not (Sender is TStringGrid) then Exit; BitBlt(TStringGrid(Sender).Canvas.Handle, Rect.Left, Rect.Top, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top, FBg.Canvas.Handle, Rect.Left, Rect.Top, SRCCOPY); if gdSelected in State then InvertRect(TStringGrid(Sender).Canvas.Handle, Rect); r := Rect; TStringGrid(Sender).Canvas.Brush.Style := bsClear; DrawText(TStringGrid(Sender).Canvas.Handle, TStringGrid(Sender).Cells[ACol, ARow], length(TStringGrid(Sender).Cells[ACol, ARow]), r, DT_SINGLELINE or DT_VCENTER or DT_END_ELLIPSIS); end; 

Sample screenshotsSample screenshotsSample screenshots

+12
source

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.

+4
source

Yes it is possible. TStringGrid inherits from TDrawGrid and does all the drawing on its own. You can use the OnDrawCell event to create a custom drawing.

+1
source

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


All Articles