辅助TForm可以在关闭时通知MainForm吗?

时间:2019-06-10 14:20:57

标签: windows c++builder vcl rad-studio

我正在使用Windows Studio VCL应用程序上的RAD Studio 10。我有两种形式,Form1Unit1.cpp中的MainForm)和辅助形式的Form2Unit2.cpp中的)。我设法将Form2嵌入到Form1的内部。这只是一个说明问题的设置。我的真实项目有多个表单。

关闭Form2时,VCL触发Form2::OnClose()事件。知道Form2是在Form1(MainForm)中动态创建的,是否有Form1事件会在Form2关闭时触发?还是Form1里面的东西知道Form2正在关闭?

  • 我当时想定制一个OnChildFormClose这样的事件处理程序,但我做不到。
  • 在公共功能中关闭Form1时,我尝试将要在Form2上执行的代码包装起来,并在Form2::OnClose()事件中调用它,并且在一定程度上起作用了,但是如果您有多个表单,则不是一个好方法。
//FROM THE 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::FormCreate(TObject *Sender)
{
TForm2 *form2 = new TForm2(this);
form2->ManualDock(container);
form2->Show();
}
//FROM unit2.cpp
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//-----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//-----------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
}
//-----------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
Close();
}
//-----------------------------------------------------------------------

我可以用OtherFormsonClose(*Sender)Form1中实现类似Sender事件的事情,我们可以动态地进行强制转换以检查它是否为Form2,还是我错了?我希望您能提供一些指导。

2 个答案:

答案 0 :(得分:3)

您可以声明类型TCloseEvent的公共事件处理程序,例如OtherFormClose(TObject *Sender, TCloseAction &Action);的主要形式:

private:    // User declarations
   void __fastcall TForm1::OtherFormClose(TObject *Sender, TCloseAction &Action);

实现

void __fastcall TForm1::OtherFormClose(TObject *Sender, TCloseAction &Action)
{
  Action = caFree;
  TForm2 *f2 = dynamic_cast<TForm2 *>(Sender);
  if (f2) {
  ShowMessage(String("Form2 closing")); //Do stuff
  }

}

(或使用Sender检查哪种形式)

然后,当您在代码中创建其他表单时,例如Form2,您分配

  TForm2 *form2 = new TForm2(this);
  form2->OnClose = OtherFormClose;
  // etc

答案 1 :(得分:0)

好吧,我在阅读thisthisthisthis之后发现了一些有趣的东西。

因此,基本上,VCL Delphi / C ++ Builder应用程序使用Windows窗体消息进行通信,我们可以覆盖虚拟函数WndProc来捕获特定消息,但是它必须是某些唯一消息,因为VCL使用很多信息,如果您不注意,事情可能会崩溃;这将转换为主窗体上的自定义事件处理程序。

所以我做的是:

  • 将MainForm句柄传递给构造函数中的Form2,以保存在Form2私有var上,并且仅用于消息传递。
  • 生成用于标记消息以使其突出的特定ID
  • 覆盖WndProc并过滤具有特定ID的消息,以便我们知道Form2正在关闭。

对其进行测试并奏效,也许有人有更好的主意。

//From unit2.h---------------------------------------------------------
class TForm2 : public TForm
{
__published:    // IDE-managed Components
    TButton *Button1;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private:    // User declarations
    HWND mfhandle;
public:     // User declarations
    __fastcall TForm2(TComponent* Owner, HWND mainformhandle);

};
//From unit2.cpp---------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm2 *Form2;
const UINT uiMyCopyDataID = RegisterWindowMessage(TEXT("MyCopyDataID"));

__fastcall TForm2::TForm2(TComponent* Owner,HWND mainformhandle)
    : TForm(Owner)
{
mfhandle = mainformhandle;
}

void __fastcall TForm2::Button1Click(TObject *Sender)
{
Close();
}

void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
//Notify the mainForm and say Hey I am closing now
PostMessage(mfhandle, uiMyCopyDataID, 0, 0);
}
//From unit1.h---------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
TPanel *container;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormUnDock(TObject *Sender, TControl *Client, TWinControl *NewTarget,
      bool &Allow);
private:    // User declarations
protected:
void __fastcall TForm1::WndProc(TMessage &Message);  //Added THIS
public:     // User declarations
__fastcall TForm1(TComponent* Owner);
};
//From unit1.cpp-------------------------------------------------------
const UINT uiMyCopyDataID = RegisterWindowMessage(TEXT("MyCopyDataID"));

void __fastcall TForm1::WndProc(TMessage &Message)
{
    if (Message.Msg == uiMyCopyDataID)
    {
    //Do SomeThing here
    ShowMessage("Form2 is closing");

    }

    TForm::WndProc(Message);
}

好的,到目前为止,它可以正常工作,并且自定义消息必须在WM_USER(0x0400-0x7FFF)范围内。