C++ Builder XE4 on Windows 7pro
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)
http://qiita.com/7of9/items/c0e4bcf9a3eae51a3525
において、TLabelの動的生成を行った。
今やりたいのは、TPanel内のすべてのTLabelを別のTPanelにコピーすること。
とりあえず位置、Font、Caption、Visibleをコピー。
code
static bool isTLabel(TControl *srcPtr) { return srcPtr->ClassName() == L"TLabel"; }
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TLabel *work;
TLabel *srcPtr;
// copy all the TLabels from Panel1 to Panel2
for(int idx=0; idx < Panel1->ControlCount; idx++) {
if (isTLabel(Panel1->Controls[idx]) == false) {
continue;
}
srcPtr = (TLabel *)Panel1->Controls[idx];
work = new TLabel( this );
work->Name = L"test" + IntToStr(idx);
work->Parent = Panel2;
work->Height = srcPtr->Height;
work->Width = srcPtr->Width;
work->Left = srcPtr->Left;
work->Top = srcPtr->Top;
work->Font = srcPtr->Font; /* Font */
work->Caption = srcPtr->Caption;
work->Visible = srcPtr->Visible;
}
}
コンポーネントがTLabel型かどうかのチェックはClassName()で確認。
SOに質問したが、ヘルプで見つけた。
(http://qiita.com/7of9/items/e601b8fb10a2a40423e3)
400個のコピー時間
400個のコンポーネントを動的コピーするとどれくらい重いのかは気になる。
実際にやってみたところ、ButtonOnClick()での処理では600msecだった。
FormCreate()時の処理では1900msecかかる。
デバッグ
動的コピーしたものがどういう名前になったかのデバッグは以下で行う。
dstPtr->Hint = dstPtr->Name;
dstPtr->ShowHint = true;
ComponentCountが増えない
上記でコピーしたのはいいが、ComponentCountがコピー前と同じだった。
なんらかの追加処理が必要なのか?
作り直した処理にて以下のthisに相当するものが、別のコンポーネントを指してたのが原因だった。
work = new TLabel( this );
関連 http://stackoverflow.com/questions/9918013/why-the-componentcount-property-of-my-tgroupbox-returns-0