Автор: LG Team
Дата сообщения: 23.10.2011 23:38
кому всё ещё интересно, как изменить шрифт у StringGrid'a..
достал этим вопросом Конопку (:, предлагаю его решение:
[more]
How do you change the font size of a TStringGrid in FireMonkey?
This is a very good question. Sounds simple enough, but it turns out that it is anything but simple. However, before diving into what is needed to accomplish the above, we first need to setup our TStringGrid which is a bit different in FMX. We'll start by creating a new FireMonkey HD application and drop a TStringGrid on the form. Next, we need to add one or more columns to the grid. To do this, right-click on the TStringGrid and select "Add Item" from the context menu. This will create a new TStringColumn to the grid. With one or more TStringColumn instances created, we can use the Cells property at runtime to populate the grid. For example,
procedure TForm21.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[ 0, 0 ] := 'Cell00';
StringGrid1.Cells[ 1, 0 ] := 'Cell10';
StringGrid1.Cells[ 0, 1 ] := 'Cell01';
StringGrid1.Cells[ 1, 1 ] := 'Cell11';
end;
Getting back to the original question of modifying the font size of the grid cells. I first tried to modify the style using the Style Designer, but there is no style associated with the cells at the grid level. I then tried the TStringColumn level, and there is no style at all for that. When I looked at the source code I saw that the TStringColumn manages a list of CellControls, which for the TStringGrid is a list of TTextCell instances. And TTextCell is a direct descendant of TEdit.
Well, changing the text size is pretty straight forward for a TEdit, you just modify the Font.Size property. However, this got me thinking about what if I wanted to modify the text color of the TEdit and not just the Font.Size or Font.FamilyName. This is not so simple. In looking at the Style for the TEdit, you will see that there is not TText element that you would typically find (e.g. in a TLabel or TGroupBox). Instead, there is a 'content' style, which appears to be what I want, but there are no child elements of 'content'. Again, looking at the source code, I determined that the style element called 'foreground' is what I want. So, the trick is to modify the Brush property of the 'foreground' style element using the Style Designer. Once I did this, my TEdit was displaying colored text.
This is all well and good, but going back to the StringGrid, we don't have a style element to access in the Style Designer to make this kind of change. So, my next approach was to try to modify the text color in code. When I looked at the source code, I discovered that the 'foreground' style element is a TBrushObject and is used to populate the property of the FontFill public property. And FontFill is used to draw the text.
Well, now we're getting somewhere. Or, so I thought. I dropped another TEdit (Edit2) on the form and I created an OnCreate event handler on my FMX form and added the following code:
procedure TForm21.FormCreate(Sender: TObject);
begin
Edit2.FontFill.Color := claBlueViolet;
end;
I then ran my app, fully expecting to see blue colored text in my TEdit. Unfortunately, nothing changed at all. The color of the text in the Edit2 was still black. Stepping through the code revealed that the TEdit will override the FontFill property values with the values defined in the style whenever ApplyStyle is called. Setting the FontFill.Color property in the FormCreate event is too early. It just gets overridden. However, ApplyStyle is protected, and I certainly did not want to have to create a new descendant simply to change the font color.
What I needed was a way to hook into the ApplyStyle process so that I make changes to the style. This is where the OnApplyStyleLookup event comes in. I deleted the above Edit2.FontFill.Color assignment from the FormCreate event handler and created a new event handler for Edit2's OnApplyStyleLookup event as shown:
procedure TForm21.Edit2ApplyStyleLookup(Sender: TObject);
begin
Edit2.FontFill.Color := claBlueViolet;
end;
Now, when I run my app, Edit2 does indeed have blue-colored text.
Of course, the question still remains how to do this for the TStringGrid. I had tried all kinds of combinations of trying to access the style elements of the grid and the columns and did not make any progress. However, with the above information, I had a new approach. What if I could handle the OnApplyStyleLookup event for the individual cells, like I did for the stand-alone TEdit?
The trick, of course, is to get access to the individual cell controls (i.e. the TTextCell instances) and not just the strings in the Cells property. To do this, we can use the CellControlByRow method of a grid column. Below is my updated FormCreate event handler. After initializing a few of the Cells to show some actual text, I then iterate through the grid's Columns and for each column I use the CellControlByRow method to get the TTextCell instance. This allows me to assign a new event handler for the OnApplyStyleLookup event.
procedure TForm21.FormCreate(Sender: TObject);
var
Col, Row: Integer;
CellCtrl: TStyledControl;
begin
StringGrid1.Cells[ 0, 0 ] := 'Cell00';
StringGrid1.Cells[ 1, 0 ] := 'Cell10';
StringGrid1.Cells[ 0, 1 ] := 'Cell01';
StringGrid1.Cells[ 1, 1 ] := 'Cell11';
for Col := 0 to StringGrid1.ColumnCount - 1 do
begin
for Row := 0 to StringGrid1.RowCount - 1 do
begin
CellCtrl := StringGrid1.Columns[ Col ].CellControlByRow( Row );
if ( CellCtrl <> nil ) and ( CellCtrl is TTextCell ) then
TTextCell( CellCtrl ).OnApplyStyleLookup := GridCellApplyStyleLookup;
end;
end;
end;
The GridCellApplyStyleLookup event is a TNotifyEvent, meaning that it takes a single Sender parameter of type TObject. Fortunately, the Sender parameter for this event is the TStyledObject that needs to be styled. In this case, Sender is a TTextCell. Therefore, in the implementation below, I check to make sure that Sender is not nil and that it is indeed a TTextCell instance. I then use that instance to modify the FontFill and Font properties.
procedure TForm21.GridCellApplyStyleLookup( Sender: TObject );
var
TC: TTextCell;
begin
if ( Sender <> nil ) and ( Sender is TTextCell ) then
begin
TC := TTextCell( Sender );
TC.FontFill.Color := claBlueViolet;
TC.Font.Family := 'Times New Roman';
TC.Font.Style := [ TFontStyle.fsBold ];
TC.Font.Size := 14;
end;
end;
Now, when I run the app, all of the cells of the string grid display their text in blue-violet using a 14 point Times New Roman font. Using the same technique would also allow me setup different OnApplyStyleLookup event handlers to change the settings between columns, etc.
A final note regarding the FMX TStringGrid. The header cells for the grid are NOT handled by the Cells property. The text displayed in the grid's header is managed by each TStringColumn's Header property.
--
Ray Konopka
[/more]
черех ж..у конечно, но хоть как-то..
а отображение db таблицы вообще геморрой ещё тот.