简体   繁体   English

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

[英]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.我想更改 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. RRUZ:您的程序是正确的并且有效,但在我的程序中不起作用。

My procedure:我的程序:

x is a global array of integer x 是 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.当我使用带有以下代码的 Canvas.Brush.Color 时,Canvas.Brush.Color 不起作用。 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);

The Rafael link contains all which you need, using the OnDrawCell event is the way to paint the cells of a StrignGrid. Rafael 链接包含您需要的所有内容,使用OnDrawCell事件是绘制 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;

I used these codes, translated to C++.我使用了这些代码,翻译成 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.在“StringGrid1”中,属性“DefaultDrawing”必须为 FALSE 才能正常工作。

  2. The "Canvas" object must be fully qualified: ie. “画布” object 必须完全合格:即。 StringGrid1->Canvas->Font->Color =clBlack. 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);
        }
    }
}
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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM