如何更改窗体上所有控件的标题?[C++生成器]

How to change the caption of all controls on a form? [C++ Builder]

本文关键字:C++ 标题 窗体 何更改 控件      更新时间:2023-10-16

我想将标题设置为所有控件(Tlabel,Tbutton,Teditlabel,Tbitbtn,TGroupBox等(和所有具有语言文件标题的组件(TMenuItems,TActions(。

我的问题是,Caption 在 TComponent、TControl 甚至 TWinControl 中都不公开。更重要的是,一些"通用"控件,如TLabel/TBitBtn,甚至不是从TWinControl派生出来的。

例:

void SetCaptionAll(TComponent *container)
{
for (int i = 0; i < container->ComponentCount; i++)
{
TComponent *child = container->Components[i];
child->Caption = ReadFromFile;    <-- This won't work. Caption is private
}
}

最重要的是:我不想使用宏(我认为这就是所谓的宏(:

#define GetCtrlCaption(p)
{ code here }

因为这不可调试。

我需要C++生成器示例,但德尔福也被接受。

适用于所有 TControl 后代:

for i := 0 to ControlCount - 1  do
Controls[i].SetTextBuf('CommonText');

要遍历所有控件,包括像面板这样的子控件,可以使用递归遍历:

procedure SetControlText(Site: TWinControl; const s: string);
var
i: Integer;
begin
for i := 0 to Site.ControlCount - 1  do begin
Site.Controls[i].SetTextBuf(PWideChar(s));
if Site.Controls[i] is TWinControl then
SetControlText(TWinControl(Site.Controls[i]), s);
end;
end;
begin
SetControlText(Self, 'CommonText');

对于像TMenuItems这样的组件,你可以使用 RTTI - 检查组件是否具有Caption, Text等属性并设置新字符串。

使用旧式方法的德尔福RTTI示例(新的RTTI自D2010起可用(。不确定它是否适用于生成器

uses... TypInfo
if IsPublishedProp(Site.Controls[i], 'Caption') then
SetStrProp(Site.Controls[i], 'Caption', 'Cap');