in my current project I have to put small BitMaps in several cells of a
StringGrid.
Has anybody got a solution?
Thanks in advance
Al
> in my current project I have to put small BitMaps in several cells of a
> StringGrid.
You'll have to use the DrawCell method. There are many examples of
drawing cells around in the various groups. Check dejanews.com for
examples, but for now here's one possible way.
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col,
Row: Longint; Rect: TRect; State: TGridDrawState);
var
s: string;
begin
s := (Sender as TStringGrid).Cells[Col, Row];
// Draw Image1.Picture.Bitmap in all Rows in Col 1
if Col = 1 then
with (Sender as TStringGrid) do
begin
// Clear current cell rect
Canvas.FillRect(Rect);
// Draw the image in the cell
Canvas.Draw(Rect.Left, Rect.Top, Image1.Picture.Bitmap);
// Draw the text in the cell
Canvas.TextOut(Rect.Left + Image1.Width + 2, Rect.Top, s);
end;
end;
Another way to do this is to replace Image1.Picture.Bitmap with an
TImageList and let TImageList.Draw method draw the image. This is handy
since you can specify the image index you want drawn and also
centralizes all you images in one TImageList rather than a bunch of
TImages. The nice thing about letting the image list draw on the canvas
is that it takes care of masking the image (i.e. transparency if you
want it).
Eg.
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col,
Row: Longint; Rect: TRect; State: TGridDrawState);
var
s: string;
begin
s := (Sender as TStringGrid).Cells[Col, Row];
// Draw ImageList Index in all Rows in Col 1
if Col = 1 then
with (Sender as TStringGrid) do
begin
// Clear current cell rect
Canvas.FillRect(Rect);
// Draw image index 0 in ImageList1 in the cell
ImageList1.Draw(Canvas, Rect.Left, Rect.Top, 0);
// Draw the text in the cell
Canvas.TextOut(Rect.Left + Image1.Width + 2, Rect.Top, s);
end;
end;
HTH,
Robert