gpt4 book ai didi

C++ Windows : Saving the line of an edit control into a vector of TCHAR

转载 作者:行者123 更新时间:2023-11-28 08:13:42 25 4
gpt4 key购买 nike

下面的代码工作正常:

int iLength = SendMessage(hWndEdit, EM_LINELENGTH, 0, 0);
if(iLength > 0) {
TCHAR* szBuffer = new TCHAR[iLength+1];
iLength = SendMessage(hWndEdit, EM_GETLINE, 0, (LPARAM)szBuffer);
szBuffer[iLength] = TEXT('\0');
MessageBox(hWnd, szBuffer, TEXT("Edit content"), MB_OK);
delete[] szBuffer;
}

现在我想用 std::vector<TCHAR> 做同样的事情:

int iLength = SendMessage(hWndEdit, EM_LINELENGTH, 0, 0);
if(iLength > 0) {
std::vector<TCHAR> data(iLength+1, TEXT('\0'));
iLength = SendMessage(hWndEdit, EM_GETLINE, 0, (LPARAM)&data[0]);
MessageBox(hWnd, &data[0], TEXT("Edit content"), MB_OK);
}

它编译,但在 iLength = SendMessage(hWndEdit, EM_GETLINE, 0, (LPARAM)&data[0]);变量 iLength将始终设置为 0,因此,我的 vector 中不会写入任何内容。

这里有什么问题?提前致谢。

最佳答案

来自 the documentation :

A pointer to the buffer that receives a copy of the line. Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer.

关于C++ Windows : Saving the line of an edit control into a vector of TCHAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8342175/

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