windows Win32 多行编辑控件在 SetWindowText() 上丢失回车

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1940766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:37:13  来源:igfitidea点击:

Win32 multiline edit control loses carriage returns on SetWindowText()

windowswinapivisual-c++controls

提问by sharptooth

In my C++ Win32 GUI application I have a dialog with an edit control created from a dialog template:

在我的 C++ Win32 GUI 应用程序中,我有一个对话框,其中包含从对话框模板创建的编辑控件:

EDITTEXT   IDC_EDIT_Id, X, Y, W, H,
    ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN | WS_VSCROLL

Whenever I manually input multiline text with carriage returns and call GetWindowText()the retrieved text is broken into lines with CRand LFcharacters as expected. However when I try to put the same text back into the edit control with SetWindowText()the control displays that text as a single string.

每当我手动输入带有回车符的多行文本并调用GetWindowText()检索到的文本时,它都会按预期分成带有CRLF字符的行。但是,当我尝试将相同的文本放回编辑控件时SetWindowText(),控件将该文本显示为单个字符串。

Why does it exhibit such behaviour and how do I workaround this?

为什么它会表现出这种行为,我该如何解决这个问题?

回答by Brian R. Bondy

When you put the text back with SetWindowText, please make sure you are using \r\n for your newlines.

当您使用 SetWindowText 放回文本时,请确保您使用 \r\n 作为换行符。

Works fine for me.

对我来说很好用。

This will display the text on 2 lines:

这将在 2 行上显示文本:

GetDlgItem(IDC_EDIT1)->SetWindowText(_T("Hello\r\nWorld!"));

Hello
World!


世界你好!

This will display the text on 1 line:

这将在 1 行显示文本:

GetDlgItem(IDC_EDIT1)->SetWindowText(_T("Hello\nWorld!"));

HelloWorld!

你好,世界!