StringGridのFontや大きさを変更するには?


ない  2012-08-06 22:06:56  No: 42716

StringGridのFontや大きさが変えれません。

VCLの場合、DrawCellで記述して実現していたのですが、
FMXの場合、よく分かりません。

ComboBoxやTListBoxでは、ListBox1.ListItems[i].Fontで
変更できるのですが・・・。

よろしくお願いします。


FMX  2012-08-07 02:38:09  No: 42717

TTextCellを使ってください。
アライメント、色、Size、Style、フォントなど指定できます。
記述はOnPaintでいけるでしょう。


FMX  2012-08-07 03:04:03  No: 42718

参考までに・・・
procedure TForm1.StringGrid1Paint(Sender: TObject; Canvas: TCanvas;
  const ARect: TRectF);
var
  i,j :Integer;
begin

    for i := 0 to StringGrid1.ColumnCount-1 do
    begin
      for j := 0 to StringGrid1.RowCount-1 do
      begin
        if Assigned(TTextCell(StringGrid1.Columns[i].CellControlByRow(j))) then
        begin
          with TTextCell(StringGrid1.Columns[i].CellControlByRow(j)) do
          begin
            if (i mod 2 = 0) and (j mod 2 = 0) then
            begin
              TextAlign := TTextAlign.taCenter;//アライメント
              Font.Size := 16;
              //FontFill.Color := claBlueViolet; {色}
              //Font.Family := 'Times New Roman';{フォント}
              //Font.Style := [ TFontStyle.fsBold ];{スタイル}
            end else begin
              TextAlign := TTextAlign.taLeading;
              Font.Size := 11;
            end;
          end;
        end;
      end;
    end;
end;


ない  2012-08-07 22:26:03  No: 42719

ありがとうございます。

教えて頂いてもらったのですが・・・。
サンプルソースを参考にやってみましたが、
ただ記述するだけでは駄目でした。
StringGridがフォーカスを受け取ったタイミング時にしか
反映されませんでした。
Buttonの処理で、VisibleをTrueにし、Realignしてもできなかったです。


ロビンズ  2012-10-10 13:05:32  No: 42720

FireMonkeyではすべてのセルに TTextCell(TEdit) が割り当てられます。
この各TTextCellを格納する配列が、
TColumn で protected FCellControls: array of TStyledControl;
として宣言されています。
要は、この FCellControls[] にアクセルできればよいわけですが、
方法が2つあります。

方法1
単純な StringGrid であれば、
StringGrid内 の 各StringColumn はそれぞれ、
Children[], ChildrenCount
Components[], ComponentCount のプロパティを持っていますが、
この Children[], Components[] に FCellControls[] がセットされます。
(ChildrenCount, ComponentCount は、StringGrid.RowCount)
以下コードを記述。

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    StringColumn1: TStringColumn;
    procedure FormCreate(Sender: TObject);
  private
    { private 宣言 }
  public
    { public 宣言 }
  end;

procedure TForm1.FormCreate(Sender: TObject);
{///////////////////////////////////////////////////////////////
  FormCreate
///////////////////////////////////////////////////////////////}
var I: Integer;
begin
  inherited;
  with StringColumn1 do begin
    for I := 0 to ComponentCount-1 do
    if Components[I] is TTextCell then begin
      TTextCell(Components[I]).Font.Family := 'メイリオ';
      TTextCell(Components[I]).Font.Size := 14;
      TTextCell(Components[I]).TextAlign := TTextAlign.taCenter;
    end;
  end;
end;

方法2
TStringColumn を新たに定義して FCellControls を可視化する。

type
  TStringColumn = class(FMX.Grid.TStringColumn)
    function GetCellControl(Index: Integer): TTextCell;
  public
    property CellControls[Index: Integer]: TTextCell
                                      read GetCellControl;
  end;

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    StringColumn1: TStringColumn;
  private
    { private 宣言 }
  public
    { public 宣言 }
  end;

以下、省略。(忙しいので、あとは自分でお考えください)


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加