【问题标题】:C++ Builder/VCL, Add images to string gridC++ Builder/VCL,将图像添加到字符串网格
【发布时间】:2016-01-26 12:25:16
【问题描述】:
您好,我有一个问题。
我正在编写一个游戏作为我的毕业项目,我一直在向 StringGrid 添加图像,这是一个 2D 益智游戏。
我发现我必须使用函数OnDrawCell,
我尝试对其进行编辑,但我不知道它的外观或实际效果如何。
我想要的是:如果我有,例如 cell[0][0] 中有字母“W”,我想显示墙的图片。
感谢您提供的任何帮助。等你回答,我会谷歌到那时。
【问题讨论】:
标签:
c++
image
c++builder
tstringgrid
【解决方案1】:
在启动时,加载包含所需墙壁图片的图像。然后,在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);
...
}