TStringGridのTPopupColumnの文字サイズの変更

解決


Moe  2025-09-11 06:20:42  No: 152141  IP: [192.*.*.*]

お世話になります。
Delphi12.0のFMXでアプリを開発しようと思っていますが、困った点がありまして質問させて頂きます。
TStringGridでTPopupColumnを使用しなくてはならないのですが、文字サイズがどうしてもデフォルトのままで変更が効きません。
ChatGPTと何度かやり取りしまして(※結構動かないコードを提示されますね)、最終的に出されたコードは次の通りなのですが、これでも反映されない感じです。
何かヒントを頂けますと幸いです。
<ChatGPTから出されたコード>
const
  COL_POPUP  = 0;   // 対象の TPopupColumn の列インデックス
  FONT_SIZE  = 16;  // フォントサイズ
  ITEM_HEIGHT = 28; // ドロップダウンのアイテム高さ


procedure TForm1.StringGrid1CreateCustomEditor(Sender: TObject;
  const Column: TColumn; var Control: TStyledControl);
var
  PB : TPopupBox;
  PC : TPopupColumn;
  CurText: string;
begin
  if Column.Index <> COL_POPUP then
    Exit;

  PC := Column as TPopupColumn;

  PB := TPopupBox.Create(StringGrid1);
  PB.Stored := False;

  // スタイルのサイズ上書きを許可(サイズはスタイルに従わない)
  PB.StyledSettings := PB.StyledSettings - [TStyledSetting.Size];

  // 選択肢を列の Items からコピー
  PB.Items.Assign(PC.Items);

  // イベント(★通常のメソッドを割り当てる)
  PB.OnApplyStyleLookup := PopupBoxApplyStyle; // ドロップダウン/表示部の見た目調整

  // 現在セルの値で初期選択を合わせる
  CurText := StringGrid1.Cells[Column.Index, StringGrid1.Row];
  PB.ItemIndex := PB.Items.IndexOf(CurText);

  Control := PB;
end;

procedure TForm1.PopupBoxApplyStyle(Sender: TObject);
var
  PB : TPopupBox;
  Txt: TText;
  LB : TListBox;
  i  : Integer;
  It : TListBoxItem;
  ItText: TText;
begin
  PB := TPopupBox(Sender);
  // セルに見えている文字('text' リソース)のフォント
  Txt := PB.FindStyleResource('text') as TText;
  if Txt <> nil then
    Txt.Font.Size := FONT_SIZE;

  // ドロップダウンの ListBox と各アイテムのフォント・高さ
  LB := PB.FindStyleResource('listbox') as TListBox;
  if LB <> nil then
  begin
    LB.ItemHeight := ITEM_HEIGHT;
    for i := 0 to LB.Count - 1 do
    begin
      It := LB.ItemByIndex(i) as TListBoxItem;
      if It <> nil then
      begin
        ItText := It.FindStyleResource('text') as TText;
        if ItText <> nil then
          ItText.Font.Size := FONT_SIZE;
      end;
    end;
  end;
end;

編集 削除
Moe  2025-09-11 06:33:22  No: 152142  IP: [192.*.*.*]

追記になりますが、上で提示したコードのLBの値はnilでした。
つまり、listboxというリソースが見つからないという事でしょうか。。。

編集 削除
AAAAA  2025-09-11 08:37:11  No: 152143  IP: [192.*.*.*]

StringGrid の Scale 変更したら?

編集 削除
Moe  2025-09-11 09:58:26  No: 152144  IP: [192.*.*.*]

AAAAA さん
有難うございます。
Scale変更で全てサイズが変わるのですね。。。ここに気付きませんでした。

編集 削除
Moe  2025-09-11 22:08:34  No: 152145  IP: [192.*.*.*]

その後のご報告になります。
以前にPopupBoxはStyleが効かないため、オリジナルのコンポーネント(TMmPopupBox)を作成した事を思い出しました。
そこで、これを次の様に使用してみたところ、スタイル(指定の文字サイズなど)が適用されたポップアップを表示することができました。
procedure TForm1.StringGrid1CreateCustomEditor(Sender: TObject;
  const Column: TColumn; var Control: TStyledControl);
var
  PB : TMmPopupBox;
  PC : TPopupColumn;
  CurText: string;
begin
  if Column.Index <> COL_POPUP then
    Exit;

  PC := Column as TPopupColumn;

  PB := TMmPopupBox.Create(StringGrid1);
  PB.Stored := False;
 :::

なのですが、コードが面倒なのもあり、今回はAAAAAさんからコメント頂いた方法で作成しようと思います。

編集 削除