1

My problem is with simple edit box. It is defined this way:

hEditIn=CreateWindowEx(WS_EX_CLIENTEDGE,
            L"EDIT",
            L"",
            WS_CHILD|WS_VISIBLE|ES_MULTILINE|
            ES_AUTOVSCROLL|ES_AUTOHSCROLL,
            50,
            120,
            400,
            200,
            hWnd,
            (HMENU)IDC_EDIT_IN,
            GetModuleHandle(NULL),
            NULL);

After that, when i call SendMessage like this:

SendMessage(hEditIn,
                            WM_SETTEXT,
                            NULL,
                            (LPARAM)L"Connected\r\n");

SendMessage(hEditIn,
                            WM_SETTEXT,
                            NULL,
                            (LPARAM)L"TESTSTR");

I get only last message instead of first message and second in new line.

This is also problematically because I want to display "Connected" every time in new line if serv retreive WM_ACCEPT message.

ak44
  • 11
  • 1
  • 3
  • 5
    `WM_SETTEXT` is called that way because it, erm, sets the text, not appends it. If you want the latter behaviour, consider reading the docs on [`WM_GETTEXTLENGTH`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632628.aspx) and [`WM_GETTEXT`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632627.aspx) – skink May 08 '14 at 17:18

1 Answers1

3

WM_SETTEXT replaces the entire contents of the Edit control with the new text. To append new text onto the existing text, use EM_SETSEL to move the caret to the end of the existing text, then use EM_REPLACESEL to insert the new text at the current caret position.

void appendTextToEdit(HWND hEdit, LPCWSTR newText)
{
    int TextLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
    SendMessage(hEdit, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
    SendMessage(hEdit, EM_REPLACESEL, FALSE, (LPARAM)newText);
}

appendTextToEdit(hEditIn, L"Connected\r\n");
appendTextToEdit(hEditIn, L"TESTSTR");
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    I've always been scared to use this technique, since the user might be interacting with the edit box and changing the selection at the same time you're appending. – Mark Ransom May 08 '14 at 19:00
  • 2
    User interaction goes through the message queue. This code does not allow the message queue to process new messages. So there is no way the user can interrupt this code (unless you are calling this code in a worker thread, which you should not do. Send a message with the text to the main thread, then call this code in that message handler). However, this code can interrupt the user if it is called in between user-generated messages. The simple way to handle that is to use `EM_GETSEL` to retrieve the user's current selection, then use `EM_SETSEL` to restore it after you have appended the text. – Remy Lebeau May 09 '14 at 01:21