简体   繁体   中英

Coloring cells in C++ builder, StringGrid

I tried to do this:

void __fastcall TTetrisGame::DrawGrid1DrawCell(TObject *Sender, int ACol, int ARow,
    TRect &Rect, TGridDrawState State)
{
    this->Canvas->Brush->Color=clBlue;
    this->Canvas->FillRect(Rect);
}

But it is a really weird result. I put my StringGrid in the middle of the window but I can't see any Blue color. Instead, it is transparent. I see a colored blue Grid in the right top corner of my window.

What am I doing wrong?

How can I color each cell individually?

You are painting on the wrong Canvas .

Inside your OnDrawCell event handler, this refers to the parent Form, because the handler is a member of the TTetrisGame class. As such, you are painting on the Form's Canvas . You need to paint on the Grid's Canvas instead:

void __fastcall TTetrisGame::DrawGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
    DrawGrid1->Canvas->Brush->Color=clBlue;
    DrawGrid1->Canvas->FillRect(Rect);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM