【问题标题】:inserting image in stringgrid cell在字符串网格单元格中插入图像
【发布时间】:2012-03-29 11:16:51
【问题描述】:

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

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

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

有人知道怎么做吗?

【问题讨论】:

    标签: image delphi tstringgrid


    【解决方案1】:

    您必须实现 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;
    

    【讨论】:

    • +1 但我发现使用 with 作为一个不好的例子,但这并不意味着 OP 不能修改它(:
    • 改成aCanvas := (Sender as TStringGrid).Canvas,写aCanvas而不是Canvas,去掉with
    • @DorinDuminica 和 Warren,你是对的。这是我使用 with 语句的唯一情况,因为我懒得定义辅助变量。否则它应该被禁止,原因有很多。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-06-03
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    相关资源
    最近更新 更多