简体   繁体   English

如何使用Delphi XE7更改stringgrid特定单元格的颜色

[英]How to change the colour of the stringgrid particular cell using Delphi XE7

I'm using Delphi XE7 for developing Android application. 我正在使用Delphi XE7开发Android应用程序。 In that I have used TStringGrid component and then I have used StringGrid.cells[0, 0] := 'Test' And how can I change the Font colour of that particular cell which I have shown in the code. 在我已经使用TStringGrid组件,然后我已经使用StringGrid.cells[0, 0] := 'Test'和如何我可以改变我在代码已经表明,特定小区的字体颜色。 And also I have this sample code, but I can not change the font colour of the particular cell. 我也有此示例代码,但无法更改特定单元格的字体颜色。 Please anybody explain me how to change the font colour of the particular cell value. 请任何人向我解释如何更改特定单元格值的字体颜色。 And I'm using Delphi XE7 and I'm targeting Android mobile. 我使用的是Delphi XE7,我的目标是Android移动版。

Thanks.. 谢谢..

In a FireMonkey TStringGrid there are no per cell styling options. 在FireMonkey TStringGrid中,没有每个单元格样式选项。 You will either need to use a third party grid control or roll something yourself from TGrid. 您将需要使用第三方网格控件或自己从TGrid中滚动内容。

You can find plenty of material on the latter on my site at http://monkeystyler.com/guide 在我的网站上,您可以在http://monkeystyler.com/guide上找到大量有关后者的资料。

At last, I have found the solution which I required. 最后,我找到了所需的解决方案。 Please follow the Steps. 请按照步骤。 We can able to change the font color in TStringGrid itself, No need to use TGrid. 我们可以在TStringGrid本身中更改字体颜色,无需使用TGrid。 Please follow the below steps. 请按照以下步骤操作。

First assign this in FormCreate event: 首先在FormCreate事件中分配此:

  StringGrid1.DefaultDrawing := False;

then write this in StringGrid DrawColumnCell event: 然后在StringGrid DrawColumnCell事件中编写以下代码

  Canvas.fill.Color := TAlphaColorRec.Green;
  Canvas.FillText(Bounds, (Value.AsString),
    false, 100, [], TTextAlign.taLeading, TTextAlign.taCenter);

Works in XE8 as well for the TStringGrid OnDrawColumnCell event. 工程在XE8以及为TStringGrid OnDrawColumnCell事件。

Herewith an example that keeps the color on black but sets the font styling to bold. 附带一个示例,该示例将颜色保留为黑色,但将字体样式设置为粗体。 Tip, add 2 pixels padding for the font, from the left margin. 提示,从左边缘开始为字体添加2个像素填充。

var Rect : TRectF;
begin
  Rect := Bounds;
  Rect.Left := Rect.Left + 2;
  Canvas.Font.Style := [TFontStyle.fsBold];
  Canvas.Fill.Color := TAlphaColorRec.Black;
  Canvas.FillText(Rect, (Value.AsString), false, 100, [], TTextAlign.taLeading, TTextAlign.taCenter);
end;

What I missed in the beginning was not setting the DefaultDrawing to false! 一开始我错过的是没有将DefaultDrawing设置为false! After I set that, the event was accepting changes to the Canvas. 设置完之后,事件就是接受对Canvas的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM