简体   繁体   中英

Windows.h - SetWindowText Shows the CR-LF Characters

I am writing a basic program in visual c++ that allows the user to enter text and then the program flips the text and displays it for the user to copy. The program works pretty good, until you add an enter to the EDIT box. When the user clicks to flip the text, instead of going down one line, it displays the actual characters for \\r\\n.

Is there a way to display the text as should instead of the actual string itself?

Here is how I set the text:

wchar_t lpwString[4096];
int length = GetWindowTextW(text->hwnd, lpwString, 4096);
SetWindowText(text->hwnd, flipText(lpwString, length));

Here is the method flipText

LPWSTR flipText(wchar_t textEntered[], const int len) {
    wchar_t text[4096];
    wchar_t flipped[4096];
    wcsncpy_s(text, textEntered, len +1);
    wcsncpy_s(flipped, textEntered, len +1);

    for (int i = len -1, k = 0; i > -1; i--, k++)
        flipped[k] = text[i];

    return flipped;
}

"text" is just an object I created to store data for an EDIT box.

For an edit box, a return is a CR+LF sequence, when you reverse the text you are transforming it in an LF+CR, which is not recognized (it shows the individual characters). An easy way out could be to do a second pass on the reversed string and swap all the LF+CR pairs into CR+LF.

Incidentally, your flipText function is seriously broken - you are performing a useless extra copy of the original string, and you are returning a pointer to a local array, which is working only by chance. A way easier method could be just to reverse the string in-place.

Also, if you are working in C++ you should consider using std::string (or std::wstring if working with wide characters), which removes whole classes of buffers lifetime/size problems.

  1. Make sure ES_WANTRETURN style is used for Edit Box.
  2. Also you should change \\n\\r back to \\r\\n right after flipText() call.

EDIT control needs '\\r\\n' combination to break. when you flip all the text, you get \\n\\r which means nothing to windows but text.

suggestion - flip the text and replace all the \\n\\r back to \\r\\n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM