delphi:如何在字符串网格中更改单元格的颜色

时间:2011-07-15 00:20:43

标签: delphi background-color tstringgrid

我想在delphi中更改字符串网格中单元格的背景颜色(不是字体)。

只有一个单元格不是行或列。

可以吗?


RRUZ:你的程序是正确的并且有效但在我的程序中不起作用。

我的程序:

x是整数

的全局数组
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;

当我使用Canvas.Brush.Color和下面的代码时,Canvas.Brush.Color不起作用。如果我在代码下面不活动,我可以更改单元格颜色。但我需要两者。

    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);

3 个答案:

答案 0 :(得分:9)

Rafael链接包含您需要的所有内容,使用OnDrawCell事件绘制StrignGrid的单元格。检查此样本,该样本仅绘制特定单元格的背景。

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;

答案 1 :(得分:3)

我使用这些代码,翻译成C ++。有两个特定的注释,然后我会发布代码。

  1. 在“StringGrid1”中,属性“DefaultDrawing”必须为FALSE才能生效。

  2. “Canvas”对象必须是完全限定的:即。 StringGrid1-&gt; Canvas-&gt; Font-&gt; Color = clBlack。

  3. 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);
            }
        }
    }
    

答案 2 :(得分:-1)

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;