读取文件并将其添加到文本框中:字符串转换问题
Read a file and add it to a textbox : string conversion issue
我使用:
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 的文档应该是
指向以 null 结尾的字符串(即窗口文本)的指针。
这意味着 C 样式字符串,一个 char* 变量。你可以尝试传递str.c_str()一个lParam。
LPARAM
定义为:typedef LONG_PTR LPARAM
所以基本上它需要一个指向通过消息传递的一些数据的指针。
然后接收方可以根据消息类型对其进行解释。
如果要传递字符串,则应传递其基础c_str()
。
当然,请确保该字符串在消息到达之前有效:
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM) str.c_str());
相关文章:
- setlocale的C++土耳其字符串问题
- C++字符串问题-如何访问字符串元素
- 提升,从字符串问题中获得时间
- 字符串问题中的系统( "command" ) 字符串
- 从文件和字符串问题中读取
- 将C++函数转换为 C#(字符串问题)
- 有关字符串C++问题
- Visual Studio和Xcode之间的Chaiscript字符串问题
- 回文字符串问题:为什么我必须放+1而不是-1才能让这个代码工作
- Dll导入Marshal C++字符串问题
- 崩溃:字符串问题
- 正在寻找另一种解决二进制字符串问题的尝试
- C++ Win32 是字符串问题
- 迭代器字符串问题
- Python 3:Python/C API字符串问题
- 带有空格的C++字符串问题
- 有些字符串问题
- C++11 std::regex_replace()创建带有特定字符串问题的regexp
- Turbo C++的字符串问题
- 棘手的子字符串问题