TGrid를 사용하여이 작업을 수행하는 방법을 배우려고 시도했으며 Mike Sutton의 도움 덕분에이를 관리했습니다.
[배경
Changing TTextCell background colour at runtime XE4를 참조하십시오.]
는이 권리를 얻는 것을 처리하는 데, 나는 TStringGrid에 비슷한 논리를 시도하고 괜찮 았는데. stringgrid는 Grid1GetValue를 사용하지 않으므로 단순히 FormCreate의 격자에 난수를 하드 코딩했습니다.
[아래 코드에는 textcellstyle이라는 스타일이 있습니다. textcellstyle의 '배경'구성 요소에 TRectangle을 추가 했으므로 TRectangle의 'Fill'속성을 호출 할 수있었습니다. . 아직 스타일의 상단에 아직, 그것은 도움이 경우, 상기와
내 코드를 링크를 참조하십시오
Procedure TForm1.FormCreate(Sender : TObject);
begin
{ CREATE AN EXTRA COLUMN }
StringGrid1.AddObject(TFinancialColumn.CreateStringGrid1));
{ HARD-CODE THE ROWS }
StringGrid1.Cells[0,0] :='0';
StringGrid1.Cells[0,1] :='1';
StringGrid1.Cells[0,2] :='2';
StringGrid1.Cells[0,3] :='3';
StringGrid1.Cells[0,4] :='4';
StringGrid1.Cells[0,5] :='5';
StringGrid1.Cells[0,6] :='6';
StringGrid1.Cells[0,7] :='7';
StringGrid1.Cells[0,8] :='8';
StringGrid1.Cells[0,9] :='9';
StringGrid1.Cells[0,10]:='10';
{ HARD-CODE A BUNCH OF NUMBERS. NOTE THAT HASH IN FRONT OF A NUMBER IS SIMPLY A FLAG FOR IsImportant }
StringGrid1.Cells[1,0] :='-10';
StringGrid1.Cells[1,1] :='-6.86999999';
StringGrid1.Cells[1,2] :='76.0999999';
StringGrid1.Cells[1,3] :='#10.25';
StringGrid1.Cells[1,4] :='#17.2900006';
StringGrid1.Cells[1,5] :='#57.1599993';
StringGrid1.Cells[1,6] :='21.86000';
StringGrid1.Cells[1,7] :='6.17';
StringGrid1.Cells[1,8] :='27.219999';
StringGrid1.Cells[1,9] :='#32.56000';
StringGrid1.Cells[1,10]:='-1.7999';
end;
Function TFinancialColumn.CreateCellControl : TStyledControl;
begin
Result:=TFinancialCell.Create(Self);
TTextCell(Result).OnTyping:=DoTextChanged;
TTextCell(Result).OnExit :=DoTextExit;
end;
Constructor TFinancialCell.Create(AOwner : TComponent);
begin
inherited;
StyleLookup:='textcellstyle';
StyledSettings:=StyledSettings-[TStyledSetting.ssStyle,TStyledSetting.ssFontColor]; { THIS LINE MUST BE HERE TO APPLY A NEW STYLE; IT CLEARS THE 'DEFAULT' STYLE SETTINGS }
TextAlign:=TTextAlign.taTrailing;
end;
Procedure TFinancialCell.SetData(const Value : TValue);
var
F : Single;
O : TFMXObject;
S : String;
begin
S:=Value.AsString;
If Length(S)>1 then
begin
FIsImportant:=S[1]='#';
If IsImportant then
S:=Copy(Value.AsString,2,MaxInt)
else
S:=Value.AsString;
F:=StrToFloat(S);
inherited SetData(Format('%n',[F]));
FIsNegative:=F<0;
ApplyStyling;
end;
end;
Procedure TFinancialCell.ApplyStyle;
var
T : TFMXObject;
begin
inherited;
T:=FindStyleResource('rectangle1');
If T is TRectangle then
begin
If IsNegative then
begin
TRectangle(T).Fill.Color:=claRed;
end;
end;
ApplyStyling;
end;
Procedure TFinancialCell.ApplyStyling;
var
T : TFMXObject;
begin
If IsNegative then
FontColor:=claBlack
else
FontColor:=claGreen;
If IsImportant then Font.Style:=[TFontStyle.fsItalic,TFontStyle.fsBold]; { REPEAT THE ITALIC ELSE IT WILL ONLY BE BOLD, IE IT OVERWRITES THE ITALIC WITH BOLD }
If Assigned(Font.OnChanged) then
Font.OnChanged(Font);
Repaint;
end;
이 코드는 글꼴과 배경을 다시 스타일 측면에서 작동합니다. 위의 내용을 개선하기위한 제안이 있으면 환영하지만 희망적으로 도움이 될 수 있습니다.
스크롤링을 시작하자마자 스타일이 떨어져 나갑니다. 아직 해결 방법을 찾지 못했습니다. 어떤 제안이라도 대환영!
TGrid를 사용하면 쉽습니다. TStringGrid를 사용하면 불가능하거나 매우 어렵습니다 (아직 결정하지 않았습니다). –