奇怪的问题
我想动态创建窗体
代码如下
unit1.cpp
#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::Button1Click(TObject *Sender)
{
TCreateActiveForm *CreateActiveForm=new TCreateActiveForm(Application,1);
CreateActiveForm->ShowModal();
delete CreateActiveForm;
}
//-----------------------------------------------
unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//------------------------------------------------------------------------
#endif
unit2.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
__fastcall TCreateActiveForm::TCreateActiveForm(TComponent *Owner,int count):TForm(Owner,0)
{ Height=100;
Width=200;
Position=poScreenCenter;
Caption="动态创建窗体";
PopupMenu1=new TPopupMenu(this);
Exit1->Caption="退出";
Exit1->OnClick=OnExit1Click;
PopupMenu1->Items->Add(Exit1);
PopupMenu=PopupMenu1;
okBtn=new TButton(this);
okBtn->Parent=this;
okBtn->Top=32;
okBtn->Left=(Width-okBtn->Width)/2;
okBtn->Caption="退出";
okBtn->OnClick=okButtonClick;
}
void __fastcall TCreateActiveForm::OnExit1Click(TObject *Sender)
{ Close();
}
void __fastcall TCreateActiveForm::okButtonClick(TObject *Sender)
{
Close();
}
unit2.h
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#endif
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
class TCreateActiveForm: public TForm
{
private:
TButton *okBtn;
TPopupMenu *PopupMenu1;
TMenuItem *Exit1;
void __fastcall OnExit1Click(TObject *Sender);
void __fastcall okButtonClick(TObject *Sender);
public:
virtual __fastcall TCreateActiveForm(TComponent *Owner,int Count);
};
按F9运行如下
按下确定后
就不正常了,为什么?
高手指点
project1.cpp如下
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
USERES("Project1.res");
USEFORM("Unit1.cpp", Form1);
USEUNIT("Unit2.cpp");
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//---------------------------------------------------------------------------