Z
zhaochaome
Unregistered / Unconfirmed
GUEST, unregistred user!
我要在程序运行时,根据实际情况,动态的添加控件(控件的个数不定),同时,自定义一个函数,和控件的click事件建立委托,我现在问,如何在自定义的函数中得到事件的触发对象阿.我用的是BCB6,谢谢 ( 积分: 200 )<br />我要在程序运行时,根据实际情况,动态的添加控件(控件的个数不定),同时,自定义一个函数,和控件的click事件建立委托,我现在问,如何在自定义的函数中得到事件的触发对象,根据时间触发源的不同,得到触发源的属性,然后,去做一些工作。问体的代码如下:
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
//void __fastcall Button1Click(TObject *Sender);
TButton *ddddddd;
void __fastcall FormClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall dddddddClick(TObject *Sender);
private: // User declarations
//void _fastcall butclick(TObject *s);
TButton *Button[];
//MESSAGE void WMCommand(TMessage &Message);
MESSAGE void butclick(TMessage &Message);
//{ShowMessage("dddddddddddd");}
public: // User declarations
__fastcall TForm1(TComponent* Owner);
begin
_MESSAGE_MAP
MESSAGE_HANDLER(WM_COMMAND, TMessage, butclick);
END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp源码
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//#include <Unit2.h>
#include "Unit1.h"
#include"command.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TButton *Button[10];
for(int i=0;i<10;i++)
{
Button=new TButton(Form1);
Button->Parent=Form1;//指定父控件的属性
//Button->TabOrder=0;
Button->Name ="Button"+IntToStr(i);
Button->Top=Form1->Top+50*i;
Button->Left =Form1->Left+50*i;
Button->Width =50;
Button->Height =50;
//Button->TabStop =true;
Button->Caption="tt"+IntToStr(i);
Button->Font->Size=12;
Button->Visible =true;
//Button->OnClick=butclick;
//delete Button[0];
//ShowMessage(Button->Caption);
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClick(TObject *Sender)
{
//TButton *Button[10];
//int i=0 ;
//for(int i=0;i<10;i++)
// Button->Visible=true;
}
//---------------------------------------------------------------------------
//void _fastcall TForm1::butclick(TObject *s)//自定义的函数
void TForm1::butclick(TMessage &Message)//自定义的函数
{
//if(Form1->Button0->CanFoucs)
MessageBox(NULL,"提示","tishi",MB_OK);
//AnsiString str =s->ClassName().operator AnsiString().SubString(3,1);
//ShowMessage(s->ClassName());
//ShowMessage(IntToStr(s->Free()));
//this-
//ShowMessage( );
// s->GetInterfaceEntry()
//switch (str.ToInt())
//{
//case 1 :
// Form2 =new TForm2(this);
// if(Form2->ShowModal()==mrOk)
// break;
//case 2 :
// delete Form2;
//}
//int i=0;
//while(i>10)
//{
//}
//for(int j=0;j<10;j++)
//{ //dynamic_cast<TButton*>(Components[j]);
// //if (Button[j]->CanFocus())
// ShowMessage(Button[j]->Caption+"lllll");
// ShowMessage(dynamic_cast<TButton*>(Components[j])->Caption);
// }
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//Form1->CanFocus()
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::dddddddClick(TObject *Sender)
{
ShowMessage("lllllll");
}
//
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
//void __fastcall Button1Click(TObject *Sender);
TButton *ddddddd;
void __fastcall FormClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall dddddddClick(TObject *Sender);
private: // User declarations
//void _fastcall butclick(TObject *s);
TButton *Button[];
//MESSAGE void WMCommand(TMessage &Message);
MESSAGE void butclick(TMessage &Message);
//{ShowMessage("dddddddddddd");}
public: // User declarations
__fastcall TForm1(TComponent* Owner);
begin
_MESSAGE_MAP
MESSAGE_HANDLER(WM_COMMAND, TMessage, butclick);
END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp源码
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//#include <Unit2.h>
#include "Unit1.h"
#include"command.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TButton *Button[10];
for(int i=0;i<10;i++)
{
Button=new TButton(Form1);
Button->Parent=Form1;//指定父控件的属性
//Button->TabOrder=0;
Button->Name ="Button"+IntToStr(i);
Button->Top=Form1->Top+50*i;
Button->Left =Form1->Left+50*i;
Button->Width =50;
Button->Height =50;
//Button->TabStop =true;
Button->Caption="tt"+IntToStr(i);
Button->Font->Size=12;
Button->Visible =true;
//Button->OnClick=butclick;
//delete Button[0];
//ShowMessage(Button->Caption);
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClick(TObject *Sender)
{
//TButton *Button[10];
//int i=0 ;
//for(int i=0;i<10;i++)
// Button->Visible=true;
}
//---------------------------------------------------------------------------
//void _fastcall TForm1::butclick(TObject *s)//自定义的函数
void TForm1::butclick(TMessage &Message)//自定义的函数
{
//if(Form1->Button0->CanFoucs)
MessageBox(NULL,"提示","tishi",MB_OK);
//AnsiString str =s->ClassName().operator AnsiString().SubString(3,1);
//ShowMessage(s->ClassName());
//ShowMessage(IntToStr(s->Free()));
//this-
//ShowMessage( );
// s->GetInterfaceEntry()
//switch (str.ToInt())
//{
//case 1 :
// Form2 =new TForm2(this);
// if(Form2->ShowModal()==mrOk)
// break;
//case 2 :
// delete Form2;
//}
//int i=0;
//while(i>10)
//{
//}
//for(int j=0;j<10;j++)
//{ //dynamic_cast<TButton*>(Components[j]);
// //if (Button[j]->CanFocus())
// ShowMessage(Button[j]->Caption+"lllll");
// ShowMessage(dynamic_cast<TButton*>(Components[j])->Caption);
// }
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//Form1->CanFocus()
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::dddddddClick(TObject *Sender)
{
ShowMessage("lllllll");
}
//