Hi all,
I am trying to customize the drawing of the cells of a TStringGrid. I need certain cells that contain hex values to have a different background color. I have written the method below which is called for the OnDrawCell event.
It works fine except when the cell is selected. In this case, the background reverts back to the default white color instead of the hex color.
Does anyone know how I can fix this problem?
void UT_CustomStringGridDrawCell(TStringGrid *StringGrid,
int Col,
int Row,
TRect &Rect,
TGridDrawState State,
bool ValidCell,
bool HexCell)
{
AnsiString Text;
if ( State.Contains(gdFixed) )
{
//Fixed row.
StringGrid->Canvas->Brush->Color =
clBtnFace;
StringGrid->Canvas->Font->Color =
ValidCell ? clWindowText :
RDCFG_INVALID_COLOR;
StringGrid->Canvas->FillRect(Rect);
Frame3D(StringGrid->Canvas,
Rect,
clBtnHighlight,
clBtnShadow,
1);
}
else if (HexCell)
{
//Hex Input Cell
StringGrid->Canvas->Brush->Color =
RDCFG_HEX_COLOR;
if (State.Contains(gdSelected))
{
StringGrid->Canvas->Font->Color =
ValidCell ? clHighlightText :
RDCFG_INVALID_COLOR;
}
else
{
StringGrid->Canvas->Font->Color =
ValidCell ?
StringGrid->Font->Color :
RDCFG_INVALID_COLOR;
}
StringGrid->Canvas->FillRect(Rect);
}
else if (State.Contains(gdSelected))
{
//Selected cell.
StringGrid->Canvas->Brush->Color =
clHighlight;
StringGrid->Canvas->Font->Color =
ValidCell ? clHighlightText :
RDCFG_INVALID_COLOR;
StringGrid->Canvas->FillRect(Rect);
}
else
{
//Normal.
StringGrid->Canvas->Brush->Color =
StringGrid->Color;
StringGrid->Canvas->Font->Color =
ValidCell ? StringGrid->Font->Color :
RDCFG_INVALID_COLOR;
StringGrid->Canvas->FillRect(Rect);
}
Text = StringGrid->Cells[Col][Row];
StringGrid->Canvas->TextRect(Rect,
Rect.Left,
Rect.Top,
Text);
}
I am trying to customize the drawing of the cells of a TStringGrid. I need certain cells that contain hex values to have a different background color. I have written the method below which is called for the OnDrawCell event.
It works fine except when the cell is selected. In this case, the background reverts back to the default white color instead of the hex color.
Does anyone know how I can fix this problem?
void UT_CustomStringGridDrawCell(TStringGrid *StringGrid,
int Col,
int Row,
TRect &Rect,
TGridDrawState State,
bool ValidCell,
bool HexCell)
{
AnsiString Text;
if ( State.Contains(gdFixed) )
{
//Fixed row.
StringGrid->Canvas->Brush->Color =
clBtnFace;
StringGrid->Canvas->Font->Color =
ValidCell ? clWindowText :
RDCFG_INVALID_COLOR;
StringGrid->Canvas->FillRect(Rect);
Frame3D(StringGrid->Canvas,
Rect,
clBtnHighlight,
clBtnShadow,
1);
}
else if (HexCell)
{
//Hex Input Cell
StringGrid->Canvas->Brush->Color =
RDCFG_HEX_COLOR;
if (State.Contains(gdSelected))
{
StringGrid->Canvas->Font->Color =
ValidCell ? clHighlightText :
RDCFG_INVALID_COLOR;
}
else
{
StringGrid->Canvas->Font->Color =
ValidCell ?
StringGrid->Font->Color :
RDCFG_INVALID_COLOR;
}
StringGrid->Canvas->FillRect(Rect);
}
else if (State.Contains(gdSelected))
{
//Selected cell.
StringGrid->Canvas->Brush->Color =
clHighlight;
StringGrid->Canvas->Font->Color =
ValidCell ? clHighlightText :
RDCFG_INVALID_COLOR;
StringGrid->Canvas->FillRect(Rect);
}
else
{
//Normal.
StringGrid->Canvas->Brush->Color =
StringGrid->Color;
StringGrid->Canvas->Font->Color =
ValidCell ? StringGrid->Font->Color :
RDCFG_INVALID_COLOR;
StringGrid->Canvas->FillRect(Rect);
}
Text = StringGrid->Cells[Col][Row];
StringGrid->Canvas->TextRect(Rect,
Rect.Left,
Rect.Top,
Text);
}