如何让动态产生的控件移动,且响应自义事件
日期:2006-06-01 荐:
如何让动态产生的控件移动,且响应自义事件//---------------------------------------------------------------------------#define No 3#include <vcl.h>#pragma hdrstop#include "Unit1.h"//---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;TButton *Btn[No];//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------------------------------------------void __fastcall TForm1::FormDragDrop(TObject *Sender, TObject *Source, int X, int Y){ TPoint a; a.x = X; a.y = Y; ScreenToClient( a ); dynamic_cast<TButton*>(Source)->Left = X; dynamic_cast<TButton*>(Source)->Top = Y;}//---------------------------------------------------------------------------void __fastcall TForm1::FormDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State, bool &Accept){ Accept = true;}//---------------------------------------------------------------------------void __fastcall TForm1::MyButtonClick(TObject *Sender){ TButton *tt=(TButton*)Sender; for (int i=1;i<=No;i ) { if (Btn[i]==tt) { Btn[i]->Cursor=crHandPoint; ShowMessage("OK"); } }}void __fastcall TForm1::FormShow(TObject *Sender){ int i_LeftPostion=10, int i_TopPostion=10; int i_wLen=5 ; for (int i=1;i<=No;i ) { Btn[i] = new TButton( this ); Btn[i]->DragMode = dmAutomatic; // 这里加一句,不响应事件???,不加控件不能动 Btn[i]->Parent = Form1; Btn[i]->Left=i_LeftPostion; //显示位置 Btn[i]->Top=i_TopPostion; Btn[i]->Width=23; //控件大小 Btn[i]->Height=20;//22 Btn[i]->Caption=IntToStr(i); Btn[i]->OnClick=MyButtonClick; i_LeftPostion=i_LeftPostion Btn[i]->Width i_wLen ; }}//---------------------------------------------------------------------------移动控件void __fastcall TForm1::Panel1MouseDown(TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y){if( mbLeft == Button ) { SetCaptureControl(NULL); SendMessage(((TPanel*)Sender)->Handle,WM_NCLBUTTONDOWN,HTCAPTION,0); }}void __fastcall TForm1::FormCreate(TObject *Sender){ TPanel *panel = new TPanel(this); .... panel->OnMOuseDown = Panel1MouseDown;//指定它的响应鼠标按下事件的代码}
标签: