簡體   English   中英

如何在 TGrid 的單元格中渲染 TBitmap 圖像?

[英]How to render a TBitmap image in a cell of a TGrid?

我在TGrid中渲染數據庫表的內容,效果很好。 現在我想在每一行上顯示一個垃圾桶的圖像作為刪除該行的按鈕。 如何才能做到這一點?

有幾種方法可以在網格中繪制圖像。 如果圖像將在運行時加載,例如從數據庫中加載,我更喜歡使用OnDrawColumnCell事件:

procedure TForm1.Grid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas;
  const Column: TColumn; const Bounds: TRectF; const Row: Integer;
  const Value: TValue; const State: TGridDrawStates);
var
  bmp: TBitmap;
begin
  if Column.Name = 'ImageColumn1' then
  begin
    bmp := ImageList1.Bitmap(Bounds.Size, Row mod ImageList1.Count);
    if assigned(bmp) then
    begin
      Grid1.BeginUpdate;
      Canvas.DrawBitmap(bmp, bmp.Bounds, Bounds, 1);
      Grid1.EndUpdate;
    end;
  end;
end;

此示例需要一個帶有多個預加載圖像的ImageList1 它將所有圖像繪制到名為ImageColumn1的列中。 要從數據庫中獲取圖像,請將該行替換為bmp訪問權限。

21 年 4 月 18 日更新:

如果您只是想顯示一個垃圾桶圖標或狀態圖標,您可以在表單上放置一個圖像列表。 添加一個TImageColumnTGlyphColumn (例如作為列號 2)並將此事件中的圖像填充到單元格中:

procedure TForm1.Grid1GetValue(Sender: TObject; const ACol, ARow: Integer;
  var Value: TValue);
begin
  if ACol = 2 then
    Value := ImageList1.Bitmap(Bounds.Size, <NumberOfBitmapWithinImageList>);
end;

對於垃圾桶圖標,您可以將刪除操作寫入以下事件方法:

procedure TForm1.Grid1CellClick(const Column: TColumn; const Row: Integer);
begin
  if Column = ImageColumn1 then
    ShowMessage('Row ' + Row.ToString + ' clicked');
end;

在事件 onDrawColumnCell 上嘗試此代碼

if stgMain.Cells[0, Row] = 'isImage' then begin
  Bounds.Location := PointF(Bounds.Location.X, Bounds.Location.Y + ((Bounds.Height - Bounds.Width) / 2));

  Bounds.Width := Bounds.Width;
  Bounds.Height := Bounds.Width;

  Canvas.Fill.Kind := TBrushKind.Bitmap;
  Canvas.Fill.Bitmap.WrapMode := TWrapMode.TileStretch;

  Canvas.FillRect(Bounds, 0, 0, AllCorners, 1);

  Canvas.Fill.Bitmap.Bitmap := FMain.img.Bitmap(Bounds.Size, 2);
end;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM