在MFC中缓慢执行AddString [重复]

时间:2021-08-26 20:22:37

I've got a dialog with several largeish combo boxes in it (maybe several hundred items apiece). There's a noticeable delay at construction while these are populated (confirmed that it's them by profiling).

我有一个对话框,里面有几个大的组合框(每个可能有几百个)。在建造这些建筑物时会有明显的延迟(通过剖析确认它们是正确的)。

My initial thought was that sorting was killing it's performance, but disabling sort and using InsertString instead doesn't seem to make things much better. I hadn't thought that it seemed like an excessive number of items - is there something else I should be doing or considering here?

我最初的想法是排序正在杀死它的性能,但禁用排序和使用InsertString似乎并没有使事情变得更好。我没有想到它似乎是一个过多的项目 - 我还应该做些什么还是在这里考虑?

The MFC calls are trivial wrappers to Win32 message calls so I don't think there's any significant overhead there.

MFC调用是Win32消息调用的简单包装器,因此我认为没有任何显着的开销。

DUPLICATE How to load a large array of strings in to an MFC combobox control fast as possible?

DUPLICATE如何尽可能快地将大量字符串加载到MFC组合框控件中?

1 个解决方案

#1


10  

You should be using CWnd::SetRedraw around your adds, to prevent the control updating all its internal state after each add.

您应该在添加时使用CWnd :: SetRedraw,以防止控件在每次添加后更新其所有内部状态。

If you're not already doing it, then do this:

如果你还没有这样做,那么这样做:

combo.SetRedraw(FALSE);

...  All the adds

combo.SetRedraw(TRUE);
combo.Invalidate();

You should also consider using the CComboBox::InitStorage function, which preallocates memory for the combo-box.

您还应该考虑使用CComboBox :: InitStorage函数,该函数为组合框预分配内存。

#1


10  

You should be using CWnd::SetRedraw around your adds, to prevent the control updating all its internal state after each add.

您应该在添加时使用CWnd :: SetRedraw,以防止控件在每次添加后更新其所有内部状态。

If you're not already doing it, then do this:

如果你还没有这样做,那么这样做:

combo.SetRedraw(FALSE);

...  All the adds

combo.SetRedraw(TRUE);
combo.Invalidate();

You should also consider using the CComboBox::InitStorage function, which preallocates memory for the combo-box.

您还应该考虑使用CComboBox :: InitStorage函数,该函数为组合框预分配内存。