動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)
やりたいこと
- 親フォームのボタン押下で子フォームを作成する
- 子フォームを実行時引数の指定によりデバッグモードにする
わからなかったこと
- 実行時引数はアプリ実行開始直後に開かれるフォーム以外も読み取れるのか?
実装
- Unit1 : 親フォーム
- Unit2 : 子フォーム
子フォームは、プロジェクト > オプション > フォーム において「自動生成フォーム」からはずして「使用可能フォーム」にしておく。
親フォーム
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
for(int idx=0; idx <= ParamCount(); idx++) {
String str = LowerCase(ParamStr(idx));
if (LowerCase(ParamStr(idx)) == L"/child") {
ShowMessage(L"Parent > /child");
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TchildForm *childPtr = new TchildForm(this);
childPtr->Show();
for(int loop=0; loop<5; loop++) {
String msg = IntToStr(loop);
OutputDebugString(msg.c_str());
Sleep(1000);
}
childPtr->Close();
childPtr->Free();
}
//---------------------------------------------------------------------------
子フォーム
Unit2.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TchildForm *childForm;
//---------------------------------------------------------------------------
__fastcall TchildForm::TchildForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TchildForm::FormShow(TObject *Sender)
{
for(int idx=0; idx <= ParamCount(); idx++) {
String str = LowerCase(ParamStr(idx));
if (LowerCase(ParamStr(idx)) == L"/child") {
ShowMessage(L"Child > /child");
}
}
}
//---------------------------------------------------------------------------
実行時引数
/child
結果
親フォーム、子フォーム、両者において実行時引数を認識できた。
親フォームOnShow
子フォームOnShow
検討事項
親フォームが実行時引数を読み取って、それを子フォームに伝えるという方法も取れるが、そうするためには以下のいずれかが必要
- 親フォームにて IsChildDebugMode()を用意
- 子フォームにて親フォームのヘッダincludeが必要になる
- 子フォームにてSetChildDebugMode()を用意
- 親フォームが子フォームを開いた後に、この関数を呼ぶ必要あり