gpt4 book ai didi

c++ - Win32 C++ : Display contents of a listbox

转载 作者:行者123 更新时间:2023-11-28 08:01:46 26 4
gpt4 key购买 nike

我正在尝试使用 SendMessage 函数显示列表框的内容。但是,当我按下按钮执行下面的代码时,调用

时我的代码中出现以下段错误
SendMessage(hwndLBox,LB_GETTEXT,k,(LPARAM)lBoxitems);

段错误(dbgheap.c):

extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;

if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;

return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}

导致此段错误的代码:

            else if(LOWORD(wParam)==ID_BUTTON_CALC){

int getLboxitems = SendMessage(hwndLBox,LB_GETCOUNT,NULL,NULL);

char * lBoxitems = new char[getLboxitems];

for(unsigned int k=0; k < getLboxitems; k++){
SendMessage(hwndLBox,LB_GETTEXT,k,(LPARAM)lBoxitems);
MessageBox(hwnd,lBoxitems,"Listbox says",NULL);
}

delete[] lBoxitems;
}

我正在使用 Visual Studio Beta 2011 来编译我的代码。

最佳答案

您需要为字符串分配空间。 char * lBoxitems = new char[getLboxitems];分配一个 char 数组,该数组的大小是列表中的项目数。我认为以下应该有效:

else if(LOWORD(wParam)==ID_BUTTON_CALC)
{
int getLboxitems = SendMessage(hwndLBox, LB_GETCOUNT, NULL, NULL);

char ** lBoxitems = new char*[getLboxitems];

for(unsigned int k = 0; k < getLboxitems; k++)
{
int textlength = SendMessage(control, LB_GETTEXTLEN, (WPARAM)k, NULL);
lBoxitems[k] = new char[textlength + 1]; // +1 for null terminator
SendMessage(hwndLBox, LB_GETTEXT, (WPARAM)k, (LPARAM)lBoxitems[k]);
MessageBox(hwnd, lBoxitems[k], "Listbox says" , NULL);
}
for (unsigned int k = 0; k < getLboxitems; k++)
{
delete [] lBoxitems[k];
}
delete[] lBoxitems;
}

但是,使用 std::vector<std::vector<char>>std::vector<std::string>会更容易:

else if(LOWORD(wParam)==ID_BUTTON_CALC)
{
int getLboxitems = SendMessage(hwndLBox, LB_GETCOUNT, NULL, NULL);

std::vector<std::string> v(getLboxitems);

for(unsigned int k = 0; k < getLboxitems; k++)
{
int textlength = SendMessage(control, LB_GETTEXTLEN, (WPARAM)k, NULL);
v[k].resize(textlength + 1);
std::string& buff = v[k];
SendMessage(hwndLBox, LB_GETTEXT, (WPARAM)k, (LPARAM)&buff[0]);
MessageBox(hwnd, v[k].c_str(), "Listbox says" , NULL);
}
}

关于c++ - Win32 C++ : Display contents of a listbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11271295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com