LoginSignup
0
1

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > 実行時引数 > 子フォームをデバッグモードにする > 子フォーム側でも実行時引数は見えている

Last updated at Posted at 2015-12-21
動作確認
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

qiita1.png

子フォームOnShow

qiita2.png

検討事項

親フォームが実行時引数を読み取って、それを子フォームに伝えるという方法も取れるが、そうするためには以下のいずれかが必要

  1. 親フォームにて IsChildDebugMode()を用意
    • 子フォームにて親フォームのヘッダincludeが必要になる
  2. 子フォームにてSetChildDebugMode()を用意
    • 親フォームが子フォームを開いた後に、この関数を呼ぶ必要あり
0
1
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
1