LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder > Form1にてShowした後に、Form2で処理をする実装 2案

Last updated at Posted at 2017-03-09
動作環境
C++ Builder XE4

以下のような処理を考える。

  • Form1で初期化処理を行う
  • その後、Form2で初期化処理を行う

参考1 http://stackoverflow.com/questions/11679235/which-is-the-best-place-to-initialize-code
参考2 Delphiでアプリケーション起動時に、モーダルダイアログを表示するには by 山本隆さん

  • 1つ目の案 > Window Messageを使う
    • 参照: 参考1,2
  • 2つ目の案 > Form1の処理後にForm2のpublic関数をForm1からコールする

code

Unit1.h

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------

#define WM_SHOW_MSG2 (WM_USER + 1)

class TForm1 : public TForm
{
__published:
    void __fastcall FormShow(TObject *Sender);
private:
    String m_message;
public:
    String __fastcall GetForm1Message(void);

    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp

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)
{
    m_message = L"";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    m_message = L"ready";

    Form2->ShowMessage();

    // PostMessage(Handle, WM_SHOW_MSG2, 0, 0);
    PostMessage(Form2->Handle, WM_SHOW_MSG2, 0, 0);
}
//---------------------------------------------------------------------------

String __fastcall TForm1::GetForm1Message(void)
{
    return m_message;
}

Unit2.h

Unit2.h
//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------

#define WM_SHOW_MSG2 (WM_APP + 1)

class TForm2 : public TForm
{
__published:
private:
public:
    void __fastcall WMshowMessage2(TMessage &msg);
    void __fastcall ShowMessage(void);

    __fastcall TForm2(TComponent* Owner);

BEGIN_MESSAGE_MAP
    MESSAGE_HANDLER(WM_SHOW_MSG2, TMessage, WMshowMessage2);
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif

Unit2.cpp

Unit2.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
    String msg1 = L"Line16: msg1=" + Form1->GetForm1Message();
    OutputDebugString(msg1.c_str());
}

void __fastcall TForm2::ShowMessage(void)
{
    String msg1 = L"Line21: msg1=" + Form1->GetForm1Message();
    OutputDebugString(msg1.c_str());
}

void __fastcall TForm2::WMshowMessage2(TMessage &msg)
{
    String msg1 = L"Line28: msg1=" + Form1->GetForm1Message();
    OutputDebugString(msg1.c_str());
}

//---------------------------------------------------------------------------

実行結果

イベントログ
デバッグ出力: Line16: msg1= プロセス Project1.exe (2344)
デバッグ出力: Line21: msg1=ready プロセス Project1.exe (2344)
デバッグ出力: Line28: msg1=ready プロセス Project1.exe (2344)

Line16の結果は、Form1の初期化処理(@FormShow)が未実施の時。
Line21の結果は「2つ目の案 > Form1の処理後にForm2のpublic関数をForm1からコールする」。
Line28の結果は「1つ目の案 > Window Messageを使う」。

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