带有firemonkey的StringGrid上的字体颜色

时间:2015-10-01 11:55:39

标签: delphi fonts firemonkey stringgrid

我正在根据数据更改背景颜色,但它使我的文字难以阅读所以我需要更改字体颜色(如果我的颜色较深则为白色)但我找不到办法它,我正在使用Delphi XE8。

if not (isSelected) then
  begin
    case StrToInt((Sender as TStringGrid).Cells[0, Row]) of
      0:
      begin
        //TTextCell(CellCtrl).StyledSettings := [];
        TTextCell(CellCtrl).FontColor := Cores[3 - auxCor - 1];
        RowColor.Color := Cores[auxCor-1];
      end;
      1:
      begin
        //TTextCell(CellCtrl).StyledSettings := [];
        TTextCell(CellCtrl).FontColor := TAlphaColors.Black;
        RowColor.Color := TAlphaColors.Red;
      end;
      2:
      begin
        //TTextCell(CellCtrl).StyledSettings := [];
        TTextCell(CellCtrl).FontColor := TAlphaColors.Black;
        RowColor.Color := TAlphaColors.Yellow;
      end;
      3:
      begin
        //TTextCell(CellCtrl).StyledSettings := [];
        TTextCell(CellCtrl).FontColor := TAlphaColors.Black;
        RowColor.Color := TAlphaColors.LightGreen;
      end;
    end;
  end;

  Canvas.FillRect(Bounds, 0, 0, [], 1, RowColor);

  TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row,
    Value, State);
  (Sender as TStringGrid).Selected := SelectedRow;

TTextCell部分没有做任何事情(我有一个类似的情况,我将颜色设置为绿色所以我需要文本为白色(如果白色最终难以阅读我会尝试其他一些色)。

核心是一个包含黑白TAlphaColors的数组

1 个答案:

答案 0 :(得分:1)

知道了:

必须在调用DefaultDrawColumnCell方法之前更改gridcolor:

procedure TFrmMainMaximized.StringGridDrawColumnCell(
  Sender: TObject; const Canvas: TCanvas; const Column: TColumn;
  const Bounds: TRectF; const Row: Integer; const Value: TValue;
  const State: TGridDrawStates);
var
  RowColor : TBrush;
  isSelected : boolean;
  FontColor : Integer;
  SelectedRow : Integer;
begin

  RowColor := Tbrush.Create(TBrushKind.Solid, TAlphaColors.Alpha);

  isSelected := ((Sender as TStringGrid).Selected = Row) and
                ((Sender as TStringGrid).ColumnIndex = Column.Index);
  SelectedRow := (Sender as TStringGrid).Selected;

  if not (isSelected) then
  begin
    case StrToInt((Sender as TStringGrid).Cells[0, Row]) of
      0:
      begin
        FontColor := Cores[3 - auxCor - 1];
        RowColor.Color := Cores[auxCor-1];
      end;
      1:
      begin
        FontColor := TAlphaColors.Black;
        RowColor.Color := TAlphaColors.Red;
      end;
      2:
      begin
        FontColor := TAlphaColors.Black;
        RowColor.Color := TAlphaColors.Yellow;
      end;
      3:
      begin
        FontColor := TAlphaColors.Black;
        RowColor.Color := TAlphaColors.LightGreen;
      end;
    end;
  end
  else
  begin
    case StrToInt((Sender as TStringGrid).Cells[0, Row]) of
      0:
      begin
        FontColor := Cores[auxCor - 1];
        RowColor.Color := Cores[3 - auxCor-1];
      end;
      1:
      begin
        FontColor := TAlphaColors.Black;
        RowColor.Color := TAlphaColors.Pink;
      end;
      2:
      begin
        FontColor := TAlphaColors.Black;
        RowColor.Color := TAlphaColors.LightYellow;
      end;
      3:
      begin
        FontColor := TAlphaColors.White;
        RowColor.Color := TAlphaColors.Green;
      end;
    end;
  end;

  Canvas.FillRect(Bounds, 0, 0, [], 1, RowColor);

  TGridAccess((Sender as TStringGrid)).GetTextSettingsControl.ResultingTextSettings.FontColor := FontColor;

  TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row,
    Value, State);
  inherited;
end;

网格中的TextSettingControl属性受到保护,因此我必须使用此函数创建一个访问类:

function TGridAccess.GetTextSettingsControl: TTextCell;
begin
  result := inherited TextSettingsControl;
end;