Font Color on a StringGrid with firemonkey

2019-08-08 17:55发布

问题:

I'm changing the background color based on the data but it makes my text hard to read so I need to change the font color (to white if I have a darker color) but I can't find a way to do it, I'm using 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;

the TTextCell portion doesn't do anything (I have an else with a similar case where I set a color to Green so I need the text to be white (if white ends up being hard to read I'll try some other colors).

Cores is an array with Black and White TAlphaColors

回答1:

Got it:

had to change the gridcolor right before calling the DefaultDrawColumnCell method:

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;

the TextSettingControl property from the grid is protected so I had to make an access Class with this function:

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