Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi : how can I change color of a cell in string grid

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 procedure is correct and works but in my procedure doesn't work.

My procedure:

x is a global array of integer

procedure TF_avalie_salon.StringGrid1DrawCell(Sender: TObject; ACol,
    ARow: Integer; Rect: TRect; State: TGridDrawState);
    var   S: string;
begin
    S := StringGrid1.Cells[ACol, ARow];
    StringGrid1.Canvas.FillRect(Rect);
    SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
    StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
    if (ARow<>0 )AND(acol<>0)AND(gridclick=true) then
    begin
        try
          gridclick:=false;
          x[acol+((strtoint(Edit_hafte.Text)-1)*7),arow]:=strtoint(StringGrid1.Cells[ACol, ARow]);
        except
          x[acol+((strtoint(Edit_hafte.Text)-1)*7),arow]:=0;
          StringGrid1.Cells[acol,arow]:='0';
          with TStringGrid(Sender) do
          begin
            Canvas.Brush.Color := clGreen;
            Canvas.FillRect(Rect);
            Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
          end;
        end;
    end;
end;

When I use Canvas.Brush.Color with below code , Canvas.Brush.Color doesn't work. If I inactive below code I can change the cells color. But I need both.

    S := StringGrid1.Cells[ACol, ARow];
    StringGrid1.Canvas.FillRect(Rect);
    SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
    StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
like image 812
Arash Avatar asked Jul 15 '11 00:07

Arash


2 Answers

The Rafael link contains all which you need, using the OnDrawCell event is the way to paint the cells of a StrignGrid. check this sample which paint only the background of an specific cell.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;  Rect: TRect; State: TGridDrawState);
begin
  if (ACol = 3) and (ARow = 2) then
    with TStringGrid(Sender) do
    begin
      //paint the background Green
      Canvas.Brush.Color := clGreen;
      Canvas.FillRect(Rect);
      Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
    end;
end;
like image 156
RRUZ Avatar answered Oct 22 '22 10:10

RRUZ


I used these codes, translated to C++. There are two specific notes, then I'll post the code.

  1. In "StringGrid1", the property "DefaultDrawing" must be FALSE for this to work.

  2. 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);
        }
    }
}
like image 5
wGraves Avatar answered Oct 22 '22 12:10

wGraves