[C++ Builder] 如何使用動態元件和事件指定?

[C++ Builder] 如何使用動態元件和事件指定?

[C++ Builder] 如何使用動態元件和事件指定?

 

資料來源:http://www.programmer-club.com.tw/ShowSameTitleN/cb/16726.html 

http://justanycode.blogspot.tw/2012/12/bcb.html

 

code2html:http://tohtml.com/

 

 

 

 

//H 檔
class TForm1 : public TForm
{
__published:	// IDE-managed Components
TButton *Button1;
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private:	// User declarations
void __fastcall NewBtnClick(TObject *Sender);
public:	// User declarations
__fastcall TForm1(TComponent* Owner);
};
//cpp 檔
TForm1 *Form1;
TButton *pNewButton[100];
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
for (int i=0; i<10; i++){
pNewButton[i] = new TButton(this);
pNewButton[i]->Parent = this;
pNewButton[i]->Font->Size = 8;
pNewButton[i]->Left = pNewButton[i]->Width*i;
pNewButton[i]->Caption ="Test"+IntToStr(i);
pNewButton[i]->Name = "Test"+IntToStr(i);
pNewButton[i]->OnClick = NewBtnClick;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NewBtnClick(TObject *Sender)
{
TButton *btn = (TButton *)Sender;
if (btn){
if (btn->Name == "Test0"){
ShowMessage("click");
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete []pNewButton;
}
//---------------------------------------------------------------------------

 

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *