0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > Formを名前から探す > frmPtr = (TForm *)Application->FindComponent(L"Form2");

Last updated at Posted at 2017-03-02
動作環境
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)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?