Link to home
Start Free TrialLog in
Avatar of ChrisLynnLaw
ChrisLynnLaw

asked on

Append new text to an EDITTEXT control

I know that
SetWindowText(hWndEditText, "Text goes here...");
changes the text in an EDITTEXT control. Now I wanna append new text to the end of (maybe start a new line) the text already in the EDITTEXT box. This is pretty like some chat tools (MSN, Yahoo! Messenger, etc.): the output window just append new messages, and doesn't flush the previously existed message text.

How do those chat tools implement this?

btw: I guess that those chat tools' output windows are actually an EDITTEXT control windows, or at least this kind of, right?
ASKER CERTIFIED SOLUTION
Avatar of s_andrew
s_andrew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MichaelS
MichaelS

It's defenetly better to use RichEdit control for that. Check it out.
or you can do:
SetText(GetText + YourText)
Avatar of ChrisLynnLaw

ASKER

Hi, thank you all for the above comments.

First, I'd say that using a RichEdit control is pretty too expensive for my application, I only need a plain textedit output window. Besides, from what I have seen, many chat applet (like the one in gamedev.net), it is apparent that the output window is just a plain textedit output window (and there are many others similar). If the simplest one works, that's the best one.

Second, I've implemented a function for appending text, but it seems not working so perfect as what I expected, the code follows:
///////////////////////////////////////////////////////////////////////////////
void AppendWindowText(HWND hWnd, LPCTSTR lpString)
{
    int iStrLen = lstrlen(lpString); // NULL terminator not included
    int iStrSize = iStrLen * sizeof(TCHAR); // NULL terminator not included
    int iCount = GetWindowTextLength(hWnd); // NULL terminator not included
    int iSize = iCount + iStrLen; // Omited the NULL terminator of existed text
    iSize += 3; /* \r\n\0 */

    TCHAR *pBuffer = new TCHAR[iSize];

    TCHAR *ptr = pBuffer;
    GetWindowText(hWnd, ptr, iCount + 1); // NULL terminator included
    ptr += iCount; // ignore the above gotten NULL terminator
    memcpy(ptr, lpString, iStrSize); // NULL terminator not included now, and will be added later
    ptr += iStrLen;
    *ptr = TCHAR('\r');
    ptr++;
    *ptr = TCHAR('\n');
    ptr++;
    *ptr = TCHAR('\0');

    SetWindowText(hWnd, pBuffer);

    delete [] pBuffer;
}
///////////////////////////////////////////////////////////////////////////////
The above code could actually append text as what I expected, but I doubt its efficiency. Furthermore, when the edittext box has been appended new text (actually, it's been refreshed all over, but seems only be appended new text), the edittext box only displays the front part of the whole text that fits into the current textedit box size (but of course, you can always use your mouse to select-view the later text that was hidden just now), but what I expect is that the textbox window scrolls up automatically when never new text being appended, so that user can always read the newest messages without doing anything. I did not add any scroll bar upon my editbox window, does scroll bar or anything else help to implement this mechanism?

And, does anyone know some standard way that those chat tools, or chat rooms use to implement the window which display (append) new messages without flushing the previous messages, and has automatic screen scrolling mechanism?

Thanks again for all your help.

b.t.w: after carefully reviewing s_andrew's comment, the way you mentioned sounds make sense, but that involves clipboard operation, right? And, is this the conventional way, or is it even better than conventional way?

Looking forward ...

int iPrevLen = GetWindowTextLength(hwndEdit);
SendMessage(hwndEdit, EM_SETSEL, iPrevLen, iPrevLen);
SendMessage(hwndEdit, EM_REPLACESEL, 0, lpszNewMessage);
...
Does the above code make sense? ( => s_andrew)
Hi, s_andrew,

before you send your comment back, I'd say that I have accepted your answer, the code follows:
///////////////////////////////////////////////////////////////////////////////
void AppendWindowText(HWND hWnd, LPCTSTR lpString)
{
    int iLength = GetWindowTextLength(hWnd);

    SendMessage(hWnd, EM_SETSEL, iLength, iLength);
    SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM) lpString);
}
///////////////////////////////////////////////////////////////////////////////
It works perfectly as what I expected. And I kinda believe that it is the very right way doing this kind of job.

I feel really indebted for you kind help.
Hi,

Could not answer to your previous comment quickly due to time zone difference. When answering I meant the solution close to the code you finally came up with. I actually did not have a ready-for-use piece of code in hand, just remembered the idea.

And... you are always welcome :-)