Reputation: 95
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.
Upvotes: 0
Views: 259
Reputation: 6556
\n\r
back to \r\n
right after flipText() call.Upvotes: 0
Reputation: 126807
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.
Upvotes: 1
Reputation: 26496
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
Upvotes: 0