C++ Builder XE4, 10.2 Tokyo > Form1からForm2へWindow Messageを飛ばす > PostMessage(Form2->Handle, WM_SHOW_MSG2, 0, 0);

More than 1 year has passed since last update.


動作環境

C++ Builder XE4

RAD Studio 10.2 Tokyo Update 2 (追記: 2017/12/28)

以下を試した。


  • 1つのアプリケーションにForm1とForm2がある

  • Form1からForm2にWindow Messageを送信する


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)


備考

Form1にてForm2->Handleを使う場合、#include "Unit2.h"とするため、依存関係が生まれる。そこでWindow Messageを使っても利点はないかもしれない。

#include "Unit2.h"せずにWindow Messageを使う限りは依存関係は切り離しできそう。

その場合は、FindWindowを使ってForm2のHWND型変数を取得することになる。