Forum > General

Using Images is StringGrid

(1/1)

Zittergie:
Hi,

I am coding an application to keep track of Magic The Gathering Cards.
I use a StringGrid to show the data, but there is one column that now shows a string like folows:

{B} for Black
{G} for Green
{R} for Red
{U} for Blue
{W} for White
{2} for Cost

anyone of these can be combined.

I would like to show  an icon (graphic, bmp, jpg or png, doesn't matter) instead of {B} or other in the StringGrid.
For the moment I show it on a seperate Panel with an image on it.

See attachment. The first grid shows the actual string,  The second grid shows what I  would like.

eny:
Use the OnDrawCell event and draw whatever you want to draw in the cell.

Zittergie:
Thanks Eny,

this is what I came up with:

procedure TFormDB.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);
var
  Bitmap    : TBitmap;
  myRect    : TRect;
  i         : longint;
  lijn, mana: string;
begin
  if acol=2 then
  begin
    Form1.LaadKaart(Stringgrid1.Cells[10,arow]);
    Mana:=Kaart.Cost;  i:=0;
    repeat
      inc(i);
      lijn:=Copy(Mana,2,pos('}',Mana)-2);
      Delete(Mana,1,pos('}',Mana));
      if fileexists(StartDir+'/Images/mana/'+lijn+'.bmp') then
      begin
        Bitmap:=TBitmap.Create;
        Bitmap.LoadFromFile(StartDir+'/Images/mana/'+lijn+'.bmp');
        myRect:=aRect;
        MyRect.Left:=Myrect.Left+2+((i-1)*14);
        myRect.Right:=Myrect.Left+14;
        Myrect.Top:=Myrect.Top+4; Myrect.Bottom:=Myrect.Bottom-4;
        StringGrid1.Canvas.StretchDraw(myRect,bitmap);
        Bitmap.free
     end;
   until Pos('{',Mana)=0;
  end;
end;

I will look to it some more in the following days.  I you have any tips on how to make this better, please let me know.
See attachment for result

eny:
You're executing fairly slow code in the OnDraw method: FileExists and especially the method BitMap.LoadFromFile.
It's better to cache the results somewhere. Either load all images beforehand, or after having loaded them the first time, store them in a list. Then, when the grid has to be redrawn, you can retrieve the bitmaps from the list in memory, instead of from disk.

Zittergie:
Hi,

I load the Bitmaps in an 'array of TBitmap' at the application start:


--- Code: ---Images: Array [0..30] of Tbitmap;  {Max number of bitmaps is 30}
--- End code ---

And changed the code to:


--- Code: ---procedure TFormDB.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState);
var
  myRect     : TRect;
  ipos, index: byte;
  lijn, mana : string;
begin
  if acol=2 then
  begin
    Form1.LaadKaart(Stringgrid1.Cells[10,arow]);
    Mana:=Kaart.Cost;  ipos:=0;
    repeat
      inc(ipos);
      lijn:=Copy(Mana,2,pos('}',Mana)-2);
      Delete(Mana,1,pos('}',Mana));
      index:=Convert(lijn);
      if index<30 then
      begin
        myRect:=aRect;
        MyRect.Left:=Myrect.Left+2+((ipos-1)*14);
        myRect.Right:=Myrect.Left+14;
        Myrect.Top:=Myrect.Top+4; Myrect.Bottom:=Myrect.Bottom-4;
        StringGrid1.Canvas.StretchDraw(myRect,images[index]);
     end;
   until Pos('{',Mana)=0;
  end;
end;   
--- End code ---

Convert is a function that returns the indexvalue of the string 'lijn' that contains the String that represents the Image needed.

Thanks again.

Navigation

[0] Message Index

Go to full version