C++ Builder/VCL,将图像添加到字符串网格

C++ Builder/VCL, Add images to string grid

你好,我有个问题。 我正在编写一个游戏作为我的毕业设计,我一直在向 StringGrid 添加图像,这是一个 2D 益智游戏。

我发现我必须使用函数 OnDrawCell, 我试图编辑它,但我不知道它应该是什么样子或它是如何工作的。

我想要的是:如果我有,例如单元格[0][0]中的字母"W",我想显示墙的图片.

感谢您提供的任何帮助。等着你的回答,我会Google到那时。

启动时,加载包含所需墙图片的图像。然后,在 OnDrawCell 事件处理程序中,检查网格的 Cells 值,如果检测到 W,则 Draw() 网格 Canvas 上的图像。没有比这更简单的了。

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);
    ...
}