c++ - 读取文件并将其添加到文本框 : string conversion issue

标签 c++ winapi sendmessage stdstring

我使用:

std::ifstream t("myfile.txt");
std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());

SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM) str); 

myfile.txt 的内容读入创建的文本框中:

HWND hwndEdit = CreateWindowEx(0, L"EDIT", NULL, WS_CHILD | WS_VISIBLE, ...)

如何解决这个错误?:

main.cpp(34) : error C2440: 'type cast' : cannot convert from 'std::string' to 'LPARAM' 
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

最佳答案

据我了解 WM_SETTEXT lparam 的文档应该是

A pointer to a null-terminated string that is the window text.

表示c风格的字符串,一个char*变量。您可以尝试将 str.c_str() 传递给 lParam。

关于c++ - 读取文件并将其添加到文本框 : string conversion issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27779067/

相关文章:

c - UpdateDriverForPlugAndPlayDevicesW 返回失败,错误代码为 ERROR_FILE_NOT_FOUND,即使文件已存在

c++ - 来自 Omnet++ 中其他项目的引用 header

c++ - operator<< 重载、 namespace 和模板

c++ - Cocos2d-x 中的 CCCallFunc 及其子类

c++ - 使用 WriteConsoleOutputA 在控制台中输出

c++ - 在类中包装一个窗口过程

c++ - STLpriority_queue实例化的疑问

multiplayer - 如何使用 firebase 向特殊在线用户发送警报消息

c# - 在特定窗口上执行 Ctrl-C 而不聚焦它

c# - 如何使用 sendmessage(C#) 向 cmd.exe 发送退格键