字体颜色上的StringGrid与firemonkey



我正在根据数据改变背景颜色,但它使我的文本难以阅读,所以我需要改变字体颜色(白色,如果我有一个较深的颜色),但我找不到一种方法来做到这一点,我使用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部分不做任何事情(我有一个类似的情况,我设置颜色为绿色,所以我需要文本为白色(如果白色最终难以阅读,我会尝试一些其他颜色)。

Cores是一个包含黑白TAlphaColors的数组

明白了:

必须在调用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;

相关内容

  • 没有找到相关文章

最新更新