其实, 要生成某一个某一个, 完全可以采用控制其Visible属性来实现, 生成的时候, 类似于让其可见。  但是呢, 提前画一个按钮到界面, 容易干扰开发人员的编辑, 于是可以采用代码来生成:

        .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);
    void __fastcall TForm1::fun(TObject *Sender);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


      .cpp文件:

 

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TButton *my = new TButton(Form1);
  my->Parent = Form1;   // 不可少

  my->Top = 100;
  my->Left = 100;
  my->Height = 125;
  my->Width = 175;
  my->Caption = "Button2";
  my->OnClick = fun;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::fun(TObject *Sender)
{
    ShowMessage("Button2 clicked");
}

 

      

 

 

 

 

 


本文转载:CSDN博客