im adding lines to a richedit control and after a while it starts to overwrite text

i create the window like this in the main window proc WM_CREATE

HINSTANCE RichEditLibHinstance = LoadLibrary("Riched32.dll");
	HWND RichEdithWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "RichEdit", "", WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_READONLY | CS_PARENTDC, 174, 0, 320, 240, hwnd, (HMENU)IDC_RICHEDIT, GetModuleHandle(NULL), NULL);

and i made a function to add a single line to the richedit control like this

void AddLinetoRichEdit(char* cpyText)
{
	CHARFORMAT cfSysText;
	ZeroMemory(&cfSysText, sizeof(CHARFORMAT));
	cfSysText.cbSize = sizeof(CHARFORMAT);
	cfSysText.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE;
	cfSysText.crTextColor = RGB(255, 255, 255);
	strcpy(cfSysText.szFaceName, "Courier New");
	cfSysText.yHeight = -11;
	int nCpyText = strlen(cpyText);
	int nLength = GetWindowTextLength(RichEdithWnd);
	SendMessage(RichEdithWnd, EM_SETSEL, nLength, nLength);
	SendMessage(RichEdithWnd, EM_REPLACESEL, false, (LPARAM)cpyText);
	SendMessage(RichEdithWnd, EM_SETSEL, nLength, nLength + nCpyText);
	SendMessage(RichEdithWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfSysText);
	SendMessage(RichEdithWnd, EM_SETSEL, nLength + nCpyText, nLength + nCpyText);
	strcpy(cpyText, "\n");
	nCpyText = strlen(cpyText);
	nLength = GetWindowTextLength(RichEdithWnd);
	SendMessage(RichEdithWnd, EM_SETSEL, nLength, nLength);
	SendMessage(RichEdithWnd, EM_REPLACESEL, false, (LPARAM)cpyText);
	SendMessage(RichEdithWnd, EM_SETSEL, nLength, nLength + nCpyText);
	SendMessage(RichEdithWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfSysText);
	SendMessage(RichEdithWnd, EM_SETSEL, nLength + nCpyText, nLength + nCpyText);
}

it does the job until the GetWindowTextLength returns somethin greater then 65,535 then it seams to start over and
starts adding the cpyText to the begining of the richedit control instead of appending it to the bottom.

ive tried calling getlasterror everywhere in my code and nothin returns anything besides "The operation completed successfully."

ive also watched nLength to see if it somehow goes back to 0 but it never does

i dont have any idea whats goin on..can anybody help me figure this out? please...

it happens when the nLength variable is higher then 65,535
i tried declaring it as
unsigned int nLength;
nothing changed

i tried
SendMessage WM_GETTEXTLENGTH
instead of the GetWindowTextLength function
nothing changed

i tried casting all wparam and lparam parameters in the SendMessge function
nothing changed

i tried using Riched20.dll instead of the riched32.dll
nothing changed

there has got to be a way to do this..somebody has got to know whats going wrong

The riched20.dll is just a wrapper for riched32.dll.

The rich edit defaults to a limit. Try sending it the following message: SendMessage(hwnd, EM_EXLIMITTEXT, 0, -1); Thereafter it shouldn't have limits.

Hope this helps.

first off thanks for the speedy reply Duoas

about the riched20.dll i read it was a wrapper and read somewhere else it was version2 of the richedit control...i was out of ideas and i have riched32 and riched20 dll on my computer so thats why i tried usin riched20 lol SendMessage(hwnd, EM_EXLIMITTEXT, 0, -1); didnt seam to work cause it still starts adding lines to the beginning of the box after its text len > 65,535
i tried settin -1 to 2147483647 but it doesnt do anything different

...several hours later...

while doin some testing i noticed something.
my program opens a file, creates a thread and starts reading the file line by line in the new thread
it adds the line to a vector of strings and when its finished reading the file it loops through the vector 1 at a time and adds each string to the richedit control then ExitThread(0)
but about halfway through the looping it starts adding lines to the beginning of the control

if i watch the output window in vc++ 6.0 for the "The thread 0xFFCA6255 has exited with code 0" line
i then start the looping of the vector of strings in the main window thread, it adds to the richedit the same way and it doesnt mess up halfway through...

so im thinking it has something to do with the thread...maybe
any ideas?

Actually, I might have that backwards. Point is, if your system is configured correctly (and most are out of the box) then it won't matter which rich edit DLL you call.

[EDIT]

It is likely that the problem is not with the rich edit control but with the program using it. There is no way to directly set a character index on huge texts with the API. You can only do it relatively.

If the calling code is keeping track of the character index in an int or long or something that is too small, then it will wrap when too much is added to it.

Hope this helps.

i figured it out...the problem was with using
EM_SETSEL
this is fine if there is less then 65535 charecters in the richedit box.
use EM_EXSETSEL othewise

EM_SETSEL is the 16bit version, limits (int)egers to 65,535
EM_EXSETSEL is the 32bit version, limits (int)egers to 4,294,967,295

hope somebody finds this usefull

Doh! :$

Nice catch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.