元件自訂事件的問題 |
答題得分者是:jow
|
jessechan
版主 發表:109 回覆:394 積分:254 註冊:2002-04-05 發送簡訊給我 |
|
gac
初階會員 發表:2 回覆:28 積分:26 註冊:2004-08-16 發送簡訊給我 |
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
[code cpp]
//--------------------------------------------------------------------------- #ifndef EditPanelH #define EditPanelH //--------------------------------------------------------------------------- #include #include #include #include //--------------------------------------------------------------------------- typedef void __fastcall (__closure *TEditChangeEvent)(TObject* Sender, int Index, TEdit* edit); //--------------------------------------------------------------------------- class PACKAGE TEditPanel : public TCustomPanel { private: TEditChangeEvent FOnEditChange; TEdit* __fastcall GetEdits(int Index); protected: TEdit* FEdits[4]; void __fastcall ArrangeEdits(); void __fastcall PanelEditChanged(TObject* Sender); public: __fastcall TEditPanel(TComponent* Owner); __property TEdit* Edits[int Index]={read=GetEdits}; __published: __property TEditChangeEvent OnEditChange={read=FOnEditChange, write=FOnEditChange}; }; //--------------------------------------------------------------------------- #endif [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "EditPanel.h" #pragma package(smart_init) //--------------------------------------------------------------------------- // ValidCtrCheck is used to assure that the components created do not have // any pure virtual functions. //--------------------------------------------------------------------------- static inline void ValidCtrCheck(TEditPanel *) { new TEditPanel(NULL); } //--------------------------------------------------------------------------- namespace Editpanel { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TEditPanel)}; RegisterComponents("Samples", classes, 0); } } //--------------------------------------------------------------------------- __fastcall TEditPanel::TEditPanel(TComponent* Owner): TCustomPanel(Owner) { Width = 127; Height = 110; for(int i=0; i<4; i ){ FEdits[i] = new TEdit(this); FEdits[i]->Parent = this; FEdits[i]->Tag = i; FEdits[i]->OnChange = &PanelEditChanged; } ArrangeEdits(); } //--------------------------------------------------------------------------- void __fastcall TEditPanel::ArrangeEdits() { TRect r = Rect(5,5,121,21); for(int i=0; i<4; i ){ FEdits[i]->BoundsRect = r; OffsetRect(r, 0, 25); } } //--------------------------------------------------------------------------- TEdit* __fastcall TEditPanel::GetEdits(int Index) { return FEdits[Index]; } //--------------------------------------------------------------------------- void __fastcall TEditPanel::PanelEditChanged(TObject* Sender) { if(FOnEditChange)FOnEditChange(this, ((TEdit*)Sender)->Tag, (TEdit*)Sender); } //--------------------------------------------------------------------------- [code cpp] //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />#include "EditPanel.h" //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: TLabel *Label1; private: protected: TEditPanel* FEditPanel; void __fastcall DO_ON_EDIT_CHANGE(TObject* Sender, int Index, TEdit* e); public: __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { FEditPanel = new TEditPanel(this); FEditPanel->Parent = this; FEditPanel->Left = 10; FEditPanel->Top = 10; FEditPanel->OnEditChange = &DO_ON_EDIT_CHANGE; } //--------------------------------------------------------------------------- void __fastcall TForm1::DO_ON_EDIT_CHANGE(TObject* Sender, int Index, TEdit* e) { Label1->Caption = IntToStr(Index) ", " e->Text; } //--------------------------------------------------------------------------- |
jessechan
版主 發表:109 回覆:394 積分:254 註冊:2002-04-05 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |