How to append text to an edit control-ZZ
From: xuyibo.net Date: 2008-01-17 10:33 AM
MFC:
void CFoo::AppendTextToEditCtrl(CEdit& edit, LPCTSTR pszText)
{
// get the initial text length
int nLength = edit.GetWindowTextLength();
// put the selection at the end of text
edit.SetSel(nLength, nLength);
// repalce the selection
edit.ReplaceSel(pszText);
}
Win32 API:
void AppendTextToEditCtrl(HWND hWndEdit, LPCTSTR pszText)
{
int nLength = GetWindowTextLength(hWndEdit);
SendMessage(hWndEdit, EM_SETSEL, (WPARAM)nLength, (LPARAM)nLength);
SendMessage(hWndEdit,EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)pszText);
}
Win32 API (using macros defined in windowsx.h):
#include
// ...
void AppendTextToEditCtrl(HWND hWndEdit, LPCTSTR pszText)
{
int nLength = Edit_GetTextLength(hWndEdit);
Edit_SetSel(hWndEdit, nLength, nLength);
Edit_ReplaceSel(hWndEdit, pszText);
}
For appending lines in a multiline edit control we must add CR/LF to the text:
void CFoo::AppendLineToMultilineEditCtrl(CEdit& edit, LPCTSTR pszText)
{
CString strLine;
// add CR/LF to text
strLine.Format(_T('\\r\\n%s'), pszText);
AppendTextToEditCtrl(edit, strLine);
}
(from: http://evotalk.net/blog/?p=131)