码迷,mamicode.com
首页 > 其他好文 > 详细

Font Color on a StringGrid with firemonkey

时间:2017-08-10 22:25:20      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:color   amp   list   other   setting   silver   agg   led   port   

 


 

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

shareimprove this question
 

 

 

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;

 

 

https://stackoverflow.com/questions/32887019/font-color-on-a-stringgrid-with-firemonkey

Font Color on a StringGrid with firemonkey

标签:color   amp   list   other   setting   silver   agg   led   port   

原文地址:http://www.cnblogs.com/zhqian/p/7341503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!