I want to change background color ( not font ) of a cell in string grid in delphi .
Just one cell not a row or a column.
Can I?
RRUZ : your pro
I used these codes, translated to C++. There are two specific notes, then I'll post the code.
In "StringGrid1", the property "DefaultDrawing" must be FALSE for this to work.
The "Canvas" object must be fully qualified: ie. StringGrid1->Canvas->Font->Color =clBlack.
CODE:
void __fastcall TForm3::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
UnicodeString uStr = "Hello";
int k, l;
char cc[100];
if(TRUE)
{
if((ACol <= 1) || (ARow <= 1))
{
StringGrid1->Canvas->Font->Color = clBlack;
StringGrid1->Canvas->Brush->Color = clBtnFace;
if(ACol == 0)
{
if(ARow > 1)
{
sprintf( cc, " %5.1f", rowLabels[ARow - 2]);
uStr = cc;
StringGrid1->Canvas->TextRect( Rect, Rect.left+2, Rect.top+2, uStr);
StringGrid1->Canvas->FrameRect(Rect);
}
}
if(ARow == 0)
{
if(ACol > 1)
{
sprintf( cc, " %5.1f", colLabels[ACol - 2]);
uStr = cc;
StringGrid1->Canvas->TextRect( Rect, Rect.left+2, Rect.top+2, uStr);
StringGrid1->Canvas->FrameRect(Rect);
}
}
}
else
{
switch (ACol%2)
{
case 0:
{
StringGrid1->Canvas->Font->Color = clRed;
StringGrid1->Canvas->Brush->Color = 0x00E1FFF9;
break;
}
case 1:
{
StringGrid1->Canvas->Font->Color = clBlue;
StringGrid1->Canvas->Brush->Color = 0x00FFEBDF;
break;
}
}
StringGrid1->Canvas->TextRect( Rect, Rect.left+2, Rect.top+2, uStr);
StringGrid1->Canvas->FrameRect(Rect);
}
}
}