tags:

views:

149

answers:

1

I have a RichEdit control in my simple application that I wish to simulate a console-like display with. I want to be able to have a buffer of x number of lines (say, 300) and whenever a line is added, I would like to also remove the oldest (top) line if the new line exceeded the threshold x. I would also like it to automatically scroll to the bottom to display the newest line when added.

I've been using SetWindowText with some success, however it occurs to me that there is likely a more efficient method of appending text to the end and removing text from the beginning without having to replace all of it every time. Is this true, and, if so, how might I go about it?

Also, how might I make it automatically scroll to the bottom of the window when new text is added?

This is using the Win32 API from C, and I'm not using the MFC version of RichEdit (just using the vanilla Win32 API on XP and Vista).

+1  A: 

To add text, you set the selection to the end of the text (EM_SETSEL), then replace the selection with the new text (EM_REPLACESEL).

To scroll to the bottom, you can send it a WM_VSCROLL with SB_BOTTOM.

Jerry Coffin
I used -1 as the start and end positions for `EM_SETSEL` to get the end and it seemed to have worked. Is this correct? Also, how might I efficiently find the first newline to remove the first line in the `RichEdit` control?
Sydius
Yes, I normally use -1 as the start and end points. I believe you should be able to find a newline with EM_FINDTEXT (you'll probably need to look for "\r\n"), though I guess I'd have to check to be sure.
Jerry Coffin