簡単な解決策は、 TFrame
インスタンスを自分自身で解放することです。 OnClick
イベントハンドラをXボタンに割り当て、 PostMessage()
を介して親の TFrame
ウィンドウにキューに入れられたメッセージを投稿させ、 TFrame
クラスは、そのメッセージが処理されたときに TFrame
インスタンスを解放するメッセージハンドラです(これは TForm :: Release()
例えば:
void __fastcall TFrame1::CloseButtonClick(TObject *Sender)
{
//CM_RELEASE is defined in Controls。hpp
PostMessage(Handle, CM_RELEASE, 0, 0);
}
void __fastcall TFrame1::WndProc(TMessage &Message)
{
if (Message。Msg == CM_RELEASE)
{
delete this;
return;
}
TFrame::WndProc(Message);
}
TFrame
インスタンスの位置を変更するなど、閉じている TFrame
を親の TForm
に通知する必要がある場合は、 TFrame
クラスのカスタム TNotifyEvent
イベントを作成し、 TForm
class TFrame1 : public TFrame
{
private:
TNotifyEvent FOnClose;
。。。
public:
。。。
__property TNotifyEvent OnClose = {read=FOnClose, write=FOnClose};
};
void __fastcall TFrame1::CloseButtonClick(TObject *Sender)
{
if (FOnClose != NULL) FOnClose(this);
PostMessage(Handle, CM_RELEASE, 0, 0);
}
void __fastcall TFrame1::WndProc(TMessage &Message)
{
if (Message。Msg == CM_RELEASE)
{
delete this;
return;
}
TFrame::WndProc(Message);
}
。
int __fastcall TForm1::AddMapCells(void)
{
Num++;
TFrame1 * MyFrame = new TFrame1(this);
MyFrame->Parent = this;
MyFrame->Name = "TFrame" + IntToStr(Num);
MyFrame->Top = 23*Num;
MyFrame->OnClose = &FrameClosed;
return Num;
}
void __fastcall TForm1::FrameClosed(TObject *Sender)
{
//Sender is the TFrame1 instance whose X button was clicked。
//It will auto-free itself after this method exits。。。
}