我有一个组合框,需要填充大量的项目,我查看了CComboBox的MSDN文档,并找到了InitStorage成员函数,其原型如下:
int CComboBox::InitStorage( int nItems, UINT nBytes );这些参数列于:
nItems:指定要添加的项数。 nBytes:指定为项字符串分配的内存量(以字节为单位)。
这听起来像是在参数中指定了的总内存量。然而,他们给出的例子与此相冲突:
// The pointer to my combo box.
extern CComboBox* pmyComboBox;
// Initialize the storage of the combo box to be 256 strings with
// about 10 characters per string, performance improvement.
int n = pmyComboBox->InitStorage(256, 10);
ASSERT(n != CB_ERRSPACE);
// Add 256 items to the combo box.
CString str;
for (int i=0;i < 256;i++)
{
str.Format(_T("item string %d"), i);
pmyComboBox->AddString( str );
}这个例子表明,nBytes参数实际上是每个字符串保留的字节数,而不是总的字节数。考虑到有一个nItems参数,这将是有意义的,因此可以很容易地计算出内存总量。
如果有人能澄清这一点,我将不胜感激。
发布于 2013-07-18 20:05:32
陈雷蒙德提供的信息表明,这是字符串所需的总额,而不是每个字符串所需的总额。
http://blogs.msdn.com/b/oldnewthing/archive/2004/06/10/152612.aspx
这将是有意义的,因为它将在字符串长度非常可变的情况下提供更多的控制。
https://stackoverflow.com/questions/17731783
复制相似问题