【发布时间】:2019-10-24 23:08:44
【问题描述】:
我正在使用 RAD Studio 10 处理 Windows VCL 应用程序。我有两个表单,Form1(Unit1.cpp 中的 MainForm)和一个辅助 Form2(Unit2.cpp)。我设法将Form2 嵌入到Form1 中。这只是说明问题的设置。我的真实项目有多个表单。
关闭Form2时,VCL触发Form2::OnClose()事件。知道Form2 是在Form1(主窗体)中动态创建的,是否有Form1 事件会在Form2 关闭时触发?或者Form1 里面的东西知道Form2 正在关闭?
- 我正在考虑自定义一个像
OnChildFormClose这样的事件处理程序,但我做不到。 - 当
Form2在公共函数中关闭并在Form2::OnClose()事件中调用时,我尝试将我想在Form1上执行的代码包装起来,并在一定程度上起作用,但它不是如果您有多个表单,这是一种很好的方法。
//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();
}
//-----------------------------------------------------------------------
我可以在Form1 中实现类似OtherFormsonClose(*Sender) 事件的Sender,我们可以动态转换以检查它是否是Form2,或者我错了?我将不胜感激。
【问题讨论】:
标签: windows c++builder vcl rad-studio