Subject Re: [IBO] Display Icon in a Grid
Author Markus Ostenried
At 11:06 Saturday, 31.01.2004 +0100, you wrote:
>Does anyone have a little example on how to display an icon in grid?
>
>I found a posting by Ales Kahánek from back in 2002 referring to a sample
>by Harald Kloman but
>didn't find the sample itself.

Hi Florian,

this is an example for the TIB_Grid.OnDrawCell event that draws a bitmap in
a cell:

procedure TW_Rez.gr_RezCliDrawCell( Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState );
var
sFieldName : String;
bmp : TBitmap;
iLeftMargin: Integer;
iTopMargin : Integer;
DrawRect : TRect;
begin
if (gdFixed in State) or
not Assigned(W_DM_Rez) or // check TDataModule
not Assigned(W_DM_Rez.D_Rez_Cli) or // check TIB_DataSource
not Assigned(W_DM_Rez.Q_Rez_Cli) or // check TIB_Query
not W_DM_Rez.Q_Rez_Cli.Prepared or
not W_DM_Rez.Q_Rez_Cli.Active or
W_DM_Rez.Q_Rez_Cli.FieldByName('ID_Rezept').IsNull or //check KeyField
(W_DM_Rez.Q_Rez_Cli.RecordCount < 1) then
begin
Exit;
end;
// locate the record that corresponds to the cell that is to be drawn
W_DM_Rez.Q_Rez_Cli.BufferRowNum := gr_RezCli.DataRow[ARow];
// get fieldname
sFieldName := UpperCase( gr_RezCli.GridFields[ACol-1].FieldName );
if (sFieldName = 'REZ_MEMO') and
W_DM_Rez.Q_Rez_Cli.BufferFieldByName('REZ_MEMO').IsNotNull then
begin
// Cell-Bereich um die Cell-Rand-Linie verkleinern
Rect.Left := Rect.Left + 1;
Rect.Right := Rect.Right - 1;
Rect.Top := Rect.Top + 1;
Rect.Bottom := Rect.Bottom - 1;
// fill background
gr_RezCli.Canvas.FillRect( Rect );
// Icon
bmp := TBitmap.Create;
try
// load the first bitmap from a TImageList
ImageList1.GetBitmap( 0, bmp );
// center in cell
iLeftMargin := ((Rect.Right-Rect.Left) - bmp.Width) div 2;
iTopMargin := ((Rect.Bottom-Rect.Top) - bmp.Height) div 2;
DrawRect := Bounds( Rect.Left+iLeftMargin, Rect.Top+iTopMargin,
bmp.Width, bmp.Height );
// draw Bitmap
gr_RezCli.Canvas.BrushCopy( DrawRect, bmp, Bounds(0, 0, bmp.Width,
bmp.Height),
bmp.TransparentColor );
finally
bmp.Free;
end;
end;
end;

HTH,
Markus