如何动态设置stringgrid单元格的颜色
1个回答
展开全部
你在就OnDrawCell事件中处理就行了,当格子需要重绘时,会自动调用这个事件,如:
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *sg = dynamic_cast <TStringGrid *>(Sender);
if (!sg) return;
if (ACol == 1 && SameText(sg->Cells[1][ARow], "1"))
sg->Canvas->Brush->Color = clRed;
else
sg->Canvas->Brush->Color = clWhite;
sg->Canvas->FillRect(Rect);
if (State.Contains(gdFocused))
sg->Canvas->Font->Color = clBlue;
sg->Canvas->TextOutA(Rect.Left + 2, Rect.Top + 2, sg->Cells[ACol][ARow]);
}
如果在代码中更改了StringGrid的值,需要刷新,就在更改值以后添加一句:
StringGrid1->Invalidate();
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *sg = dynamic_cast <TStringGrid *>(Sender);
if (!sg) return;
if (ACol == 1 && SameText(sg->Cells[1][ARow], "1"))
sg->Canvas->Brush->Color = clRed;
else
sg->Canvas->Brush->Color = clWhite;
sg->Canvas->FillRect(Rect);
if (State.Contains(gdFocused))
sg->Canvas->Font->Color = clBlue;
sg->Canvas->TextOutA(Rect.Left + 2, Rect.Top + 2, sg->Cells[ACol][ARow]);
}
如果在代码中更改了StringGrid的值,需要刷新,就在更改值以后添加一句:
StringGrid1->Invalidate();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询