MFC中动态创建控件(集合题)
我有一些自定义控件,我应该在其中创建单选按钮或复选框。子控件的计数仅在运行时可用(它加载一些从中获取此计数的文件)。所以我需要创建可变数量的控件。我应该使用哪个集合来实现此目的?
解决方案 1:只需使用 std::vector
(或 CArray
) - 不适合,因为我想使用 MFC (CButton)。当然,每次我需要这个窗口时,我都可以 Attach()
和稍后的 Detach()
处理窗口,但这会带来很大的开销。
解决方案 2:使用 std::vector
或 CArray
或 CList
或。 .. 在这种情况下,当不需要控制时,我会注意创建“新”和适当的“删除”。我很健忘:)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想使用 Count 参数读取文件,创建按钮,使用它们,然后将它们全部删除,那么我认为
CArray
就可以了。为了确保按钮被删除,您可以将 CArray 包装到一个帮助器中,例如:然后将此类的对象作为成员添加到您的自定义控件 (
m_MyButtonArrayWrapper
) 并添加按钮:如果需要为了随机添加和删除按钮,出于性能原因,
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:Then add an object of this class as a member to your custom control (
m_MyButtonArrayWrapper
) and add your buttons with: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 :))