在stringgrid单元格中插入图像

时间:2012-03-29 11:16:51

标签: image delphi tstringgrid

我在我的应用程序中使用stringgrid。数据从数据库(后端mysql)获取并显示在stringgrid中。

enter image description here

我想在每行的状态单元格中插入图像。 即。

      if status =online then -->image1
      else --->image2

任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:7)

您必须实现OnDrawCell事件。

示例:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Longint;
  Rect: TRect; State: TGridDrawState);
var
  s: string;
  aCanvas: TCanvas;
begin
  if (ACol <> 1) or (ARow = 0) then
    Exit;
  s := (Sender as TStringGrid).Cells[ACol, ARow];

  // Draw ImageX.Picture.Bitmap in all Rows in Col 1
  aCanvas := (Sender as TStringGrid).Canvas;  // To avoid with statement
  // Clear current cell rect
  aCanvas.FillRect(Rect);
  // Draw the image in the cell
  if (s = 'online') then
    aCanvas.Draw(Rect.Left, Rect.Top, Image1.Picture.Bitmap)
  else 
    aCanvas.Draw(Rect.Left, Rect.Top, Image2.Picture.Bitmap);
end;
相关问题