Font Color on a StringGrid with firemonkey

2017-08-11 08:08:03来源:cnblogs.com作者:周黔人点击

分享

 

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

delphi fonts firemonkey stringgrid
shareimprove this questionedited Oct 6 '15 at 17:33RRUZ118k12271413asked Oct 1 '15 at 11:55Ricardo Alvares Pereira Junior819
  

 

 
 

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

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台