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

前端 未结 3 1767
囚心锁ツ
囚心锁ツ 2020-12-07 01:13

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

相关标签:
3条回答
  • 2020-12-07 01:32

    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;
    
    0 讨论(0)
  • 2020-12-07 01:43

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 01:47
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
    i:integer;  
    begin
      with Sender as TStringGrid do
        begin
            Canvas.FillRect(Rect);
            DrawText (Canvas.Handle,
                PChar(Cells[ACol, ARow]),
                Length(Cells[ACol, ARow]),
                Rect, DT_WORDBREAK or DT_EXPANDTABS or DT_CENTER);
        end;
        for i:=2 to  StringGrid1.RowCount - 1 do
        if StringGrid1.Cells[3,i]='' then
          begin
            StringGrid1.Canvas.Brush.Color:=clRed;
              if ((ACol=3)and(ARow=i)) then
                begin
                  StringGrid1.Canvas.FillRect(Rect);
    
                end;
          end;
    
    
    end;
    
    0 讨论(0)
提交回复
热议问题