C++ Set Window 文本问题
C++ Set Window Text issue
好的,我有一个 file.txt,其中包含以下内容:
xzline1\n
xzline2\n
当我运行它时,window包含这个:
xzline1\nxzline2\n
而不是
xzline1
xzline2
无法识别 \n 换行符,不知道为什么。
我的window是这样定义的
LPCWSTR recordin;
HWND hEdit;
hEdit = CreateWindow(TEXT("EDIT"), NULL,
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_HSCROLL | WS_MAXIMIZE | ES_MULTILINE,
10, 10, 200, 25,
hWnd, (HMENU)NULL, NULL, NULL);
std::ifstream t("c://file.txt");
std::stringstream buffer;
buffer << t.rdbuf();
std::wstring stemp = s2ws(buffer.str());
recordin = stemp.c_str();
SetWindowText(hEdit, recordin);
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
经典 Windows 常用控件需要 DOS 行结尾,\r\n
。将所有 \n
个字符转换为 \r\n
.
可能可以将此作为快速破解:
std::wstring stemp = s2ws(buffer.str());
// quick and dirty string copy with DOS to to unix conversions
std::wstring stemp2;
for (char ch : stemp) {
if (ch == '\n') {
stemp2 += "\r";
}
stemp2 += ch;
}
recordin = stemp2.c_str();
SetWindowText(hEdit, recordin);
或者,您的 input.txt
很可能被写成带有 \r\n
行结尾的标准 Windows 文本文件,而 C++ 运行时只是转换所有这些 \r\n
个实例到 \n
个字符。如果是这种情况,您可以只打开二进制文件,这样转换就不会发生。
替换为:
std::ifstream t("c://file.txt");
有了这个:
std::ifstream t("c://file.txt", std::ios::binary);
好的,我有一个 file.txt,其中包含以下内容:
xzline1\n
xzline2\n
当我运行它时,window包含这个:
xzline1\nxzline2\n
而不是
xzline1
xzline2
无法识别 \n 换行符,不知道为什么。
我的window是这样定义的
LPCWSTR recordin;
HWND hEdit;
hEdit = CreateWindow(TEXT("EDIT"), NULL,
WS_VISIBLE | WS_CHILD | WS_BORDER | WS_HSCROLL | WS_MAXIMIZE | ES_MULTILINE,
10, 10, 200, 25,
hWnd, (HMENU)NULL, NULL, NULL);
std::ifstream t("c://file.txt");
std::stringstream buffer;
buffer << t.rdbuf();
std::wstring stemp = s2ws(buffer.str());
recordin = stemp.c_str();
SetWindowText(hEdit, recordin);
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
经典 Windows 常用控件需要 DOS 行结尾,\r\n
。将所有 \n
个字符转换为 \r\n
.
可能可以将此作为快速破解:
std::wstring stemp = s2ws(buffer.str());
// quick and dirty string copy with DOS to to unix conversions
std::wstring stemp2;
for (char ch : stemp) {
if (ch == '\n') {
stemp2 += "\r";
}
stemp2 += ch;
}
recordin = stemp2.c_str();
SetWindowText(hEdit, recordin);
或者,您的 input.txt
很可能被写成带有 \r\n
行结尾的标准 Windows 文本文件,而 C++ 运行时只是转换所有这些 \r\n
个实例到 \n
个字符。如果是这种情况,您可以只打开二进制文件,这样转换就不会发生。
替换为:
std::ifstream t("c://file.txt");
有了这个:
std::ifstream t("c://file.txt", std::ios::binary);