2008. december 28., vasárnap

How to use a TImage as the background for a TDBGrid


Problem/Question/Abstract:

I would like to have a DBGrid on a form that contains a bitmap and have the text in the cells of the grid float on the bitmap. I've tried using SetBKMode but apparently I'm using it incorrectly. Is there a separate canvas for each cell as well as the grid itself?

Answer:

Place a TImage on a form or create it off-screen and put the following (untested) code in the OnDrawColumnCell event of the grid:


procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  w, h: Integer;
begin
  with DBGrid1.Canvas do
  begin
    w := Rect.Right - Rect.Left;
    h := Rect.Bottom - Rect.Top;
    BitBlt(DBGrid1.Canvas.Handle, Rect.Left, Rect.Top, w, h,
      Image1.Picture.Bitmap.Canvas.Handle, Rect.Left, Rect.Top, SRCCOPY);
    Brush.Style := bsClear;
    TextOut(Rect.left, Rect.Top, Column.Field.AsString);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése