動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2017/12/28)
現在実装を検討している処理にて、Formの名前(String型)からTForm*ポインタを取得する必要がありそうなため方法を調べた。
- プロジェクトの構成
- Form1 (Unit1.cpp)
- Form2 (Unit2.cpp)
- Caption: L"Form2"
参考 http://stackoverflow.com/questions/25610673/delphi-find-form-by-name
Unit1.cpp
//---------------------------------------------------------------------------
# include <vcl.h>
# pragma hdrstop
# include "Unit1.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TForm *frmPtr;
frmPtr = (TForm *)Application->FindComponent(L"Form2");
if (frmPtr == NULL) {
return; // not found
}
OutputDebugString(frmPtr->Caption.c_str());
}
//---------------------------------------------------------------------------
結果
デバッグ出力: Form2 プロセス Project1.exe (5808)