[英]How to render a TBitmap image in a cell of a TGrid?
I am rendering the content of a database table in a TGrid
, which works fine.我在TGrid
中渲染数据库表的内容,效果很好。 Now I would like to show an image of a trash can on every row as a button to delete the row.现在我想在每一行上显示一个垃圾桶的图像作为删除该行的按钮。 How can this be done?如何才能做到这一点?
There are several ways to paint an image in a Grid.有几种方法可以在网格中绘制图像。 In cases, where the images will be loaded at runtime eg from a database, I prefer to use the OnDrawColumnCell
event:如果图像将在运行时加载,例如从数据库中加载,我更喜欢使用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;
This example expects an ImageList1
with several preloaded images.此示例需要一个带有多个预加载图像的ImageList1
。 It draws all images into the column with the name ImageColumn1
.它将所有图像绘制到名为ImageColumn1
的列中。 To take your images from the database, replace the line with the bmp
access.要从数据库中获取图像,请将该行替换为bmp
访问权限。
Update at 18-Apr-21: 21 年 4 月 18 日更新:
If you simply want to show a trash icon or eg a status icon, you can put an image list on the form.如果您只是想显示一个垃圾桶图标或状态图标,您可以在表单上放置一个图像列表。 Add a TImageColumn
or TGlyphColumn
(eg as column number 2) and fill the image in this event into the cell:添加一个TImageColumn
或TGlyphColumn
(例如作为列号 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;
For a trash icon, you can write your delete action into the following event method:对于垃圾桶图标,您可以将删除操作写入以下事件方法:
procedure TForm1.Grid1CellClick(const Column: TColumn; const Row: Integer);
begin
if Column = ImageColumn1 then
ShowMessage('Row ' + Row.ToString + ' clicked');
end;
try this code on event onDrawColumnCell在事件 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.