MFC中动态创建控件(集合题)

发布于 2024-08-27 15:43:10 字数 647 浏览 7 评论 0原文

我有一些自定义控件,我应该在其中创建单选按钮或复选框。子控件的计数仅在运行时可用(它加载一些从中获取此计数的文件)。所以我需要创建可变数量的控件。我应该使用哪个集合来实现此目的?

解决方案 1:只需使用 std::vector (或 CArray) - 不适合,因为我想使用 MFC (CButton)。当然,每次我需要这个窗口时,我都可以 Attach() 和稍后的 Detach() 处理窗口,但这会带来很大的开销。

解决方案 2:使用 std::vectorCArrayCList 或。 .. 在这种情况下,当不需要控制时,我会注意创建“新”和适当的“删除”。我很健忘:)

MFC 句柄映射包含指向 CButton 的指针,我不能使用简单的 CArray,因为每次他的大小增大时,它都会移动我的对象。

...问题是: 我应该使用哪个集合来包含 MFC 控件类的变量计数?

I have some custom control inside of which i should create radiobuttons or checkboxes. The count of child controls is available only at runtime (it loads some file from which it gets this count). So i need to create variable number of controls. Which collection i should use for this purpose?

Solution 1: simply use std::vector<HWND> (or CArray<HWND>) - not suitable because i want use MFC (CButton). Of course i can Attach() and later Detach() handle to window each time i need this window, but it will give big overhead.

Solution 2: use std::vector<CButton*> or CArray<CButton*> or CList<CButton*> or... In this case i take care about making 'new' and appropriate 'delete' when control is unneeded. I am forgetful :)

MFC handle map contains pointer to CButton and i can't use simple CArray<CButton>, because it will move my objects each time when his size will grow.

... and the question is:
Which collection i should use for containing variable count of MFC control classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-09-03 15:43:10

如果您只想使用 Count 参数读取文件,创建按钮,使用它们,然后将它们全部删除,那么我认为 CArray 就可以了。为了确保按钮被删除,您可以将 CArray 包装到一个帮助器中,例如:

class CMyButtonArrayWrapper
{
public:
    CMyButtonArrayWrapper();
    virtual ~CMyButtonArrayWrapper();

    void ClearArray();
    void Add(CButton* theButton);

private:
    CArray<CButton*> m_Array;
}

CMyButtonArrayWrapper::CMyButtonArrayWrapper()
{
}

CMyButtonArrayWrapper::~CMyButtonArrayWrapper()
{
    ClearArray();
}

void CMyButtonArrayWrapper::ClearArray()
{
    for (int i=0; i<m_Array.GetSize(); i++)
    {
        CButton* aButton=m_Array.GetAt(i);
        if (aButton)
            delete aButton;
    }
    m_Array.RemoveAll();
}

void CMyButtonArrayWrapper::Add(CButton* theButton)
{
    m_Array.Add(theButton);
}

然后将此类的对象作为成员添加到您的自定义控件 (m_MyButtonArrayWrapper) 并添加按钮:

CButton* aButton=new CButton;
aButton->Create( ... );
m_MyButtonArrayWrapper.Add(aButton);

如果需要为了随机添加和删除按钮,出于性能原因,CList 可能更适合。 (但是您可能不会注意到使用 CArray 的 InsertAt/RemoveAt 的性能差异,除非您的 UI 有数千个按钮。我想这更像是一件艺术品而不是用户界面:))

If you only want to read your file with the Count parameter, create your buttons, work with them and later delete them all then CArray<CButton*> is fine in my opinion. To make sure the buttons get deleted you could wrap the CArray into a helper like:

class CMyButtonArrayWrapper
{
public:
    CMyButtonArrayWrapper();
    virtual ~CMyButtonArrayWrapper();

    void ClearArray();
    void Add(CButton* theButton);

private:
    CArray<CButton*> m_Array;
}

CMyButtonArrayWrapper::CMyButtonArrayWrapper()
{
}

CMyButtonArrayWrapper::~CMyButtonArrayWrapper()
{
    ClearArray();
}

void CMyButtonArrayWrapper::ClearArray()
{
    for (int i=0; i<m_Array.GetSize(); i++)
    {
        CButton* aButton=m_Array.GetAt(i);
        if (aButton)
            delete aButton;
    }
    m_Array.RemoveAll();
}

void CMyButtonArrayWrapper::Add(CButton* theButton)
{
    m_Array.Add(theButton);
}

Then add an object of this class as a member to your custom control (m_MyButtonArrayWrapper) and add your buttons with:

CButton* aButton=new CButton;
aButton->Create( ... );
m_MyButtonArrayWrapper.Add(aButton);

If you need to add and remove buttons randomly a CList might be better suited for performance reasons. (But you won't probably notice a performance difference using InsertAt/RemoveAt of CArray, except your UI has several thousands of buttons. I guess this would be more an artwork than a user interface :))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文