在运行时从窗体删除TFrame [英] Deleting TFrame from form at run time

查看:137
本文介绍了在运行时从窗体删除TFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++ Builder创建VCL表单应用程序.现在我有一个包含许多组件的TFrame,看起来像这样...

I am using C++ Builder to create a VCL form application. Right now I have a TFrame containing a bunch of components and it looks like this...

我还有一个按钮调用添加".基本上,每当我按下表单上的添加"按钮时,都会向其中添加一个新的TFrame,并在前一个TFrame下方添加一个看起来像表格的东西.为了添加重复项,我必须在创建TFrame之前每次都对其重命名.

I also have a button call "Add". Basically every time I press that Add button on the form, a new TFrame is added to it and below the previous one making something that looks like a table. And in order to add duplicates I have to rename the TFrame each time before it is created.

    int __fastcall TForm1::AddMapCells(void)
    {
        Num++;
        TFrame1 * MyFrame = new TFrame1(Form1);
        MyFrame->Parent=Form1;
        MyFrame->Name = "TFrame" + IntToStr(Num);
        MyFrame->Top = 23*Num;
        return Num;
    }

因此TFrame的命名为TFrame1,TFrame2,TFrame3等.

So then the naming of TFrame would be TFrame1, TFrame2, TFrame3, etc.

现在的问题是我要制造它,因此每当我按下TFrame的"X"按钮时,它都会删除该TFrame,但我不确定该怎么做.我在想,也许每次创建TFrame时,我都可以重命名"X"按钮,所以它就像Button1,Button2,Button3等.然后删除程序,只需将ButtonX与TFrameX匹配以标识要删除的TFrame.例如,如果我按下按钮4,它应该与TFrame4匹配并删除TFrame4.

The problem now is I want to make it so then each time I press the 'X' button of a TFrame, it deletes that TFrame and I'm not quite sure how to do it. I was thinking maybe each time I create a TFrame I can also rename the 'X' button so then it's like Button1, Button2, Button3, etc. And then to delete the program would just match ButtonX with TFrameX to identify which TFrame to delete. For example, if I press Button 4, it should match up with TFrame4 and delete TFrame4.

我不知道如何实现这个想法.还是会有一种更简单的方法?

I don't know how to implement this idea. Or would there be an easier way of doing this?

推荐答案

一个简单的解决方案是让TFrame实例为您释放自己.将OnClick事件处理程序分配给X按钮,并使其通过PostMessage()将排队的消息发布到其父TFrame窗口,然后为TFrame类提供消息处理程序,该处理程序在该消息释放时释放TFrame实例.被处理(这是TForm::Release()方法的工作方式),例如:

A simple solution would be to let the TFrame instances free themselves for you. Assign an OnClick event handler to the X button and have it post a queued message to its parent TFrame window via PostMessage(), then give the TFrame class a message handler that frees the TFrame instance when that message is processed (this is how the TForm::Release() method works), eg:

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);
}

如果需要通知您的父级TForm关闭的TFrame(例如,重新放置较低的TFrame实例),则可以在TFrame类中公开一个自定义的TNotifyEvent事件,并让您的TForm为其分配一个事件处理程序,例如:

If you need your parent TForm to be notified of the TFrame being closed (for instance, to reposition lower TFrame instances), you can expose a custom TNotifyEvent event in the TFrame class and have your TForm assign an event handler to it, eg:

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...
}

这篇关于在运行时从窗体删除TFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆