c++ builder / window message > Show Form and Hide Form


MyEnvironment
C++ Builder XE4

The delphi implementation of show/hide form is shown by RepeatUntil
http://stackoverflow.com/questions/36589398/this-hide-works-only-once

I implemented the code in C++ Builder for my future reference.

code

common header

I used WM_APP instead of WM_USER.

myWinMsg.h
#ifndef MYWINMSGH
#define MYWINMSGH

// for Window message
static const int kWM_show = WM_APP + 1;
static const int kWM_hide = kWM_show + 1;

#endif // MYWINMSGH

code for UnitHide

UnitHide.h
//---------------------------------------------------------------------------

#ifndef UnitHideH
#define UnitHideH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include "MyWinMsg.h"
//---------------------------------------------------------------------------

class TFormHide : public TForm
{
__published:    //
    TButton *btnHide;
    void __fastcall btnHideClick(TObject *Sender);
private:    //
public:     //
    void __fastcall WMShow(TMessage &msg);
    void __fastcall WMHide(TMessage &msg);

    __fastcall TFormHide(TComponent* Owner);

BEGIN_MESSAGE_MAP
    MESSAGE_HANDLER(kWM_show, TMessage, WMShow);
    MESSAGE_HANDLER(kWM_hide, TMessage, WMHide);
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TFormHide *FormHide;
//---------------------------------------------------------------------------
#endif
UnitHide.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "UnitHide.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormHide *FormHide;
//---------------------------------------------------------------------------
__fastcall TFormHide::TFormHide(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TFormHide::WMShow(TMessage &msg)
{
    this->Show();
}

void __fastcall TFormHide::WMHide(TMessage &msg)
{
    this->Hide();
}

void __fastcall TFormHide::btnHideClick(TObject *Sender)
{
    SendMessage(Handle, kWM_hide, 0, 0);
}
//---------------------------------------------------------------------------

code for UnitShow

UnitShow.h
//---------------------------------------------------------------------------

#ifndef UnitShowH
#define UnitShowH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include "MyWinMsg.h"
//---------------------------------------------------------------------------

class TFormShow : public TForm
{
__published:
    TButton *btnShow;
    void __fastcall btnShowClick(TObject *Sender);
private:
public:
    __fastcall TFormShow(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormShow *FormShow;
//---------------------------------------------------------------------------
#endif
UnitShow.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "UnitShow.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormShow *FormShow;
//---------------------------------------------------------------------------
__fastcall TFormShow::TFormShow(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormShow::btnShowClick(TObject *Sender)
{
    HWND hWndX;

    hWndX = FindWindow(L"TFormHide", NULL);
    if (hWndX != NULL) {
        SendMessage(hWndX, kWM_show, NULL, NULL);
    } else {
        MessageBox(0, L"Window not found", L"Msg", NULL);
    }

}
//---------------------------------------------------------------------------