//这个是VC的代码,输出如下
**********************************
In TForm2.ShowModal,Press Enter to Close It!
TForm.ShowModal() END!
**********************************
**********************************
In TForm3.ShowModal,Press Enter to Close It!
TForm.ShowModal() END!
**********************************
// ConAPP.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "windows.h"
//*************************************************************************
// BCB中自带的内容
class TObject
{
public:
virtual LPCTSTR get_ClassName(){ return "TObject"; }
};
class TComponent : public TObject
{
private:
TComponent* m_AOwner;
public:
TComponent(TComponent* AOwner) : m_AOwner(AOwner) {}
virtual LPCTSTR get_ClassName(){ return "TComponent"; }
};
class TForm : public TComponent
{
public:
TForm(TComponent* AOwner) : TComponent(AOwner){}
virtual LPCTSTR get_ClassName(){ return "TForm"; }
int ShowModal()
{
std::cout<< "**********************************" <<"/n";
std::cout<< "In "
<<this->get_ClassName()<<".ShowModal,Press Enter to Close It!" <<"/n";
int ch = getchar();
std::cout<< "TForm.ShowModal() END!" <<"/n";
std::cout<< "**********************************" <<"/n";
return ch;
}
};
//在BCB中,这个需要删掉
class TApplication : public TComponent
{
public:
virtual LPCTSTR get_ClassName(){ return "TApplication"; }
};
static TApplication* Application;
TApplication* get_Application()
{
return Application;
}
//*************************************************************************
// 基于模板的帮助模板类
template<class T>
class CVclHelper
{
public:
virtual T* Create(TComponent* AOwner)
{
return new T(AOwner);
}
};
//*************************************************************************
// 实际的工作代码
typedef CVclHelper<TForm> TFormClass;
//*************************************************************
// TForm1
class TForm1 : public TForm
{
public:
TForm1(TComponent* AOwner) : TForm(AOwner){}
virtual LPCTSTR get_ClassName(){ return "TForm1"; }
void ShowForm(CVclHelper<TForm1>* FormClass);
void btnForm2Click(TObject* Sender);
void btnForm3Click(TObject* Sender);
};
typedef CVclHelper<TForm1> TForm1Class;
//*************************************************************
// TForm2
class TForm2 : public TForm1
{
public:
TForm2(TComponent* AOwner) : TForm1(AOwner){}
virtual LPCTSTR get_ClassName(){ return "TForm2"; }
};
typedef CVclHelper<TForm2> TForm2Class;
//*************************************************************
// TForm3
class TForm3 : public TForm1
{
public:
TForm3(TComponent* AOwner) : TForm1(AOwner){}
virtual LPCTSTR get_ClassName(){ return "TForm3"; }
};
typedef CVclHelper<TForm3> TForm3Class;
static TForm1* Form1;
void TForm1::ShowForm(CVclHelper<TForm1>* FormClass)
{
Form1 = FormClass->Create(Application);
Form1->ShowModal();
delete Form1;
}
void TForm1::btnForm2Click(TObject* Sender)
{
TForm2Class FormClass;
this->ShowForm((TForm1Class*) &FormClass);
}
void TForm1::btnForm3Click(TObject* Sender)
{
TForm3Class FormClass;
this->ShowForm((TForm1Class*) &FormClass);
}
//*************************************************************************
// 测试代码
int _tmain(int argc, _TCHAR* argv[])
{
TForm1* Form1 = new TForm1(Application);
Form1->btnForm2Click(Form1);
Form1->btnForm3Click(Form1);
delete Form1;
getchar();
return 0;
}