Home » C++ ProgrammingRSS

Using WinAPI, how can you pass formatted (new-line) characters to an "Edit" class child Window, pref

First I apologize if this is not the best forum for this, but I am using the VC++ software, and this is a Windows-programming question--I didn't see a place for coding in basic WinAPI...

Information:  I'm creating a patcher/installer for a current project.  Like most of these applications, mine has a window in the center of the app for displaying download/patch, etc. information as it runs through the process.  Everything's running smoothly except that the text runs together, it doesn't seem to accept escape-character formatting.  I've even taken the time to switch all variables away from wide-chars and functions into the "A" versions--using LPWSTR's and the functions for wide-characters, it seemed rare to have them accept formatted text.  Is it possible to make an "Edit" child window class accept text formatting?  Here's my current source for this portion:

void WriteInTextBox(LPSTR stringToWrite)
{
	int maxBufferSize = SendMessage(hInfoBox, EM_GETLIMITTEXT, 0, 0);
	LPSTR buffer = newchar[maxBufferSize];

	GetDlgItemTextA(hPatcher, H_INFO_BOX, buffer, maxBufferSize);

	strcat(buffer, stringToWrite);
	strcat(buffer, "\n");

	SetDlgItemTextA(hPatcher, H_INFO_BOX,buffer);
	SendMessage(hInfoBox, EM_LINESCROLL, 0, (SendMessage(hInfoBox, EM_GETLINECOUNT, 0, 0)));

	return;
}
Any help, pointers, suggestions or tips in the right direction would be GREATLY appreciated.  Thank you!
 

5 Answers Found

 

Answer 1

Use “\r\n” as a line separator. Make sure buffer is always large enough and is deleted. Appending can be also done by moving the caret to the end and executing EM_REPLACESEL (one for stringToWrite and another one for “\r\n”).

 

Answer 2

Works perfectly!  Thank you very much!!

Two questions in response to this. 
First, is the method for appending which you suggested more standard or common or appropriate?  Or is it a matter of preference?
Second, could you explain why both escape characters  are needed for this?

 

Answer 3

If you are not using MFC/ATL then the appropriate forum  is VC General.

Yes, EM_REPLACESEL is the normal way to append text  to an edit  control.

The "\r\n" line ending has always been needed by the edit control.  Probably to be compatible with standard Windows .txt files.  Such API interface details are never changed, to preserve compatibility with old programs.

 

Answer 4

I figured this was the closest thing to WinAPI here.

 

Anyway, I was actually asking the details behind why it requires both, but as far as being compatible with standard .txt, you can save information  into a text  file using just \n or \r seperate to create new lines.  I'm just curious why the edit  control needs both.

 

Answer 5

I figured this was the closest thing to WinAPI here.
 
Anyway, I was actually asking the details behind why it requires both, but as far as being compatible with standard .txt, you can save information  into a text  file using just \n or \r seperate to create new lines. I'm just curious why the edit  control needs both.
When you write to a file in text mode (the default), on Windows the \n is automatically replaced by \r\n in the file. And when you read a text file, the \r\n is replaced by \n.
 
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter