【问题标题】:How colored the Cell of StringGrid Delphi XE2?StringGrid Delphi XE2 的 Cell 是如何着色的?
【发布时间】:2014-03-18 02:05:28
【问题描述】:

我从 Delphi 开始。我对 TStringGrid 和着色单元格有疑问。我在选择时使用此代码为背景着色:

procedure TForm_Matrix.MatrizGeneralDrawCell(Sender: TObject;
  ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  ACol:=MatrizGeneral.Col;
  ARow:=MatrizGeneral.Row;
  begin
    if (RBAlto.Checked = True) then // Nivel de color ROJO - ALTO
      MatrizGeneral.Canvas.Brush.Color :=clRed;
      MatrizGeneral.Canvas.FillRect(Rect);
    if (RBMedio.Checked = True) then
      MatrizGeneral.Canvas.Brush.Color :=clYellow;
      MatrizGeneral.Canvas.FillRect(Rect);
    if (RBBajo.Checked = True) then
      MatrizGeneral.Canvas.Brush.Color :=clLime;
      MatrizGeneral.Canvas.FillRect(Rect);
  end;
end;

它的工作,但是当我尝试更改颜色时,更改所选单元格,以及第一个单元格 idk 为什么。

  1. 当我选择 3 个红色单元格时。 (工作正常)

  2. 改变另一个单元格的颜色,改变第一个单元格T.T

    http://i.stack.imgur.com/umG0r.png http://i.stack.imgur.com/1o93C.png

求助!!!

【问题讨论】:

    标签: delphi colors stringgrid


    【解决方案1】:

    如果只想对选中的单元格着色,则必须检查传入的State参数,并且仅在选中State时才绘制。

    此外,您在该例程中绘制了 3 倍单元格。只要把 MatrizGeneral.Canvas.FillRect(Rect);最后一次,每个 IF 块都不需要它。

    【讨论】:

    • 我使用这个规则为使用无线电组选择的单元格着色:如果 MatrizGeneral.Cells[ACol,ARow] '' 然后开始 case StrToInt(MatrizGeneral.Cells[ACol,ARow]) of 0 : BGColor := clRed; 1: BGColor := clYellow; 2: BGColor := clLime;否则 BGColor := clWhite;结尾;使用 MatrizGeneral 开始 Canvas.Brush.Color := BGColor; Canvas.FillRect(Rect); if (gdFocused in State) then Canvas.Font.Color := clWhite else Canvas.Font.Color := clBlack;结尾;结束;
    【解决方案2】:

    我使用这个规则为通过无线电组选择的单元格着色:

    if MatrizGeneral.Cells[ACol,ARow] <> '' then begin
    case StrToInt(MatrizGeneral.Cells[ACol,ARow]) of
      0: BGColor := clRed;
      1: BGColor := clYellow;
      2: BGColor := clLime;
    else
      BGColor := clWhite;
    end;
    with MatrizGeneral do begin
      Canvas.Brush.Color := BGColor;
      Canvas.FillRect(Rect);
    
      if (gdFocused in State) then
        Canvas.Font.Color := clWhite
      else
        Canvas.Font.Color := clBlack;
    end;
    

    结束;

    效果很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多