字符颜色与Firemonkey的StringGrid [英] Font Color on a StringGrid with firemonkey

查看:392
本文介绍了字符颜色与Firemonkey的StringGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据数据改变背景颜色,但是这使得我的文本很难阅读,所以我需要改变字体颜色(如果颜色较深,则为白色),但我找不到方法如果没有(isSelected),那么
begin
case StrToInt($)

  if ((Sender as TStringGrid).Cells [0,Row])
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

解决方案



必须更改在调用DefaultDrawColumnCell方法之前调用gridcolor:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $发送者:TObject; const Canvas:TCanvas; const Column:TColumn;
const Bounds:TRectF; const Row:Integer; const Value:TValue; $ b $ const State:TGridDrawStates);
var
RowColor:TBrush;
isSelected:boolean;
FontColor:Integer;
SelectedRow:Integer;
begin

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

isSelected:=((Sender as TStringGrid).Selec ted = Row)和
((Sender as TStringGrid).ColumnIndex = Column.Index);
SelectedRow:=(发件人为TStringGrid).Selected;

if(isSelected)then
begin
case
的StrToInt((Sender as TStringGrid).Cells [0,Row])0:
开始
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])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);
继承;
end;

网格中的TextSettingControl属性是受保护的,所以我必须使用此函数创建一个访问类: / p>

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


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

解决方案

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;

这篇关于字符颜色与Firemonkey的StringGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆