Search code examples
imagedelphitstringgrid

Delphi StringGrid with picture in background


Hi does anyone know if it is possible to display a picture as a background to a string grid, Or is anyone aware of any free Grid component that can do this.

Thanks

colin


Solution

  • You could use a TDrawGrid (or a 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 a TBitmap (in the form class, for instance), 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 Screenshot Sample Screenshot Sample Screenshot