Applications: Appending text to a multiline Edit control

Today I was developing a small utility application where I needed to append some text to a multiline edit control. When I ran through the list of Edit control messages, I could not find any that could be used to append text. WM_SETTEXT was overwriting the previous contents. And doing a WM_GETTEXT first, appending to it and then WM_SETTEXT seemed like an overkill.

 

A little bit of searching led me to EM_REPLACESEL message. This message is used to replace the currently selected text in the edit control with the specified text but there’s a bit more to it. If there is no selected text in the edit control then the specified text is inserted at the current caret position. Since what I was using was a read-only edit control this works for me but I am not sure if it will work in all cases. I wonder if we need a SETCARETPOS message. So anyways, here’s how to append text in an edit control:



HWND hWndEdit = GetDlgItem(g_hDlg, IDC_EDIT_BOX);


SendMessage(hWndEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)str);

Leave a Reply

Your email address will not be published. Required fields are marked *