How can I programmatically save event handler and then set it in C++ Builder?
I need to temporary remove TFrame's OnExit and OnEnter events, so I'm trying to do following:
declare FEnterHandler and FExit Handler:
private:
// ...
TControl *FParentControl;
(__fastcall *(__closure)(TObject*))(TObject*) FEnterHandler;
(__fastcall *(__closure)(TObject*))(TObject*) FExitHandler;
// ...
开发者_运维问答
and I intended to use them as typed downhere, but compilation failed on declaration.
__fastcall TProgressForm::TProgressForm(TComponent *O, TControl *PC)
: TForm(O), FMapProgressData()
{
FParentControl = PC;
if (FParentControl)
{
TFrame *frame = dynamic_cast<TFrame*>(FParentControl);
if (frame)
{
FEnterHandler = frame->OnEnter;
FExitHandler = frame->OnExit;
frame->OnEnter = 0;
frame->OnExit = 0;
}
FParentControl->Enabled = false;
}
}
//-------------------------------------------------------------------------
__fastcall TProgressForm::~TProgressForm()
{
if (FParentControl)
{
FParentControl->Enabled = true;
TFrame *frame = dynamic_cast<TFrame*>(FParentControl);
if (frame)
{
frame->OnEnter = FEnterHandler;
frame->OnExit = FExitHandler;
}
}
}
What am I doing wrong?
I don't know much C++Builder, but can't you just write
TNotifyEvent FEnterHandler;
TNotifyEvent FExitHandler;
? Looks much nicer and is less error-prone.
I'd add my own answer, because I found, that typedef from help is rather strange:
typedef void _fastcall (__closure *TNotifyEvent)(System::TObjectTObject Sender)
and written another, more clear.
typedef void (__closure __fastcall *TEventHandler)(TObject*);
//typedef TNotifyEvent TEventHandler;
TEventHandler FEnterHandler;
TEventHandler FExitHandler;
Of course, I recommend to use TNotifyEvent, it is really better.
精彩评论