c++ - MFC CComboBox::InitStorage 文档说明

标签 c++ winapi mfc

我有一个组合框,我需要用可能的大量项目填充它,我查看了 CComboBox 的 MSDN MFC 文档,我找到了 InitStorage成员函数,原型(prototype)如下:

int CComboBox::InitStorage( int nItems, UINT nBytes );

参数如下:

nItems: Specifies the number of items to add.

nBytes: Specifies the amount of memory, in bytes, to allocate for item strings.

这听起来像是您在 nBytes 参数中指定了total 内存量。但是,他们给出的例子与此冲突:

// 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 参数,这是有意义的,因此可以轻松计算内存总量。

如果有人能澄清这一点,我将不胜感激。

最佳答案

Raymond Chen 提供的此信息表明它是字符串所需的总金额,而不是每个字符串。

http://blogs.msdn.com/b/oldnewthing/archive/2004/06/10/152612.aspx

这是有道理的,因为它可以在字符串长度变化很大的情况下提供更多控制。

关于c++ - MFC CComboBox::InitStorage 文档说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17731783/

相关文章:

c++ - 永无止境的 Win32 消息循环

c++ - 如何在 MFC 对话框的静态区域中绘制图像?

c++ - 了解使用C++赋值时的运算符 “less”或 “greater”

c++ - boost::locale::date_time:如何从 Boost C++ 中的 date_time 对象获取数据?

windows - 访问非常长的文件名的 ntfs 流失败

c++ - CWinAppEx 未定义类

c++ - 最大化的 MFC 对话框大于监视器

c++ - 无法删除数组(我不知道的错误)

c++ - 将项目添加到队列

c++ - 如何在 Win32 上加速具有大纹理的屏幕外 OpenGL 渲染?