-2

Hello, so I have a problem. I'm programming a game as my Graduation Project and I'm stuck at adding images to StringGrid, It's a 2D Puzzle game.

What I found is that I have to use function OnDrawCell, I tried to edit it, but I don't know how it should look or how it really works.

What I want is: If I have, for example letter "W" in cell[0][0], I want to show the picture of wall instead.

I appreciate any help given. Waiting for you answers, I'll Google till then.

Steven Carlson
  • 925
  • 1
  • 10
  • 25

1 Answers1

0

At startup, load an image containing the desired wall picture. Then, in the OnDrawCell event handler, check the grid's Cells value and if W is detected then Draw() that image on the grid's Canvas. It doesn't really get any simpler than that.

class TForm1 : public TForm
{
__published:
    TStringGrid *StringGrid1;
    void __fastcall StringGrid1DrawCell(TObject* Sender,
       int ACol, int ARow, const TRect &Rect, TGridDrawState State);
private:
    Graphics::TBitmap *WallBmp;
public:
    __fastcall TForm1(TComponent *Owner);
    __fastcall ~TForm1::TForm1();
};

__fastcall TForm1::TForm1(TComponent *Owner)
    : TForm(Owner)
{
    WallBmp = new Graphics::TBitmap;
    // fill image as needed - load a file or
    // a resource, hand draw directly on
    // WallBmp->Canvas, etc...
}

__fastcall TForm1::~TForm1()
{
    delete WallBmp;
}

void __fastcall TForm1::StringGrid1DrawCell(TObject* Sender,
    int ACol, int ARow, const TRect &Rect, TGridDrawState State)
{
    ...
    if (StringGrid1->Cells[ACol][ARow] == "W")
      StringGrid1->Canvas-Draw(WallBmp, Rect.Left, Rect.Top);
    ...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770