Article ID: 109550
Article Last Modified on 7/11/2005
HWND hEdit = GetDlgItem (hDlg, ID_EDIT);
int ndx = GetWindowTextLength (hEdit);
SetFocus (hEdit);
#ifdef WIN32
SendMessage (hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);
#else
SendMessage (hEdit, EM_SETSEL, 0, MAKELONG (ndx, ndx));
#endif
Once the caret is placed at end in the edit control, you can use the
EM_REPLACESEL to append text to the edit control. An application sends
an EM_REPLACESEL message to replace the current selection in an edit
control with the text specified by the lpszReplace (lParam) parameter.
Because there is no current selection, the replacement text is inserted at
the current caret location. This example sets the selection to the end of
the edit control and inserts the text in the buffer:
#ifdef WIN32
SendMessage (hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);
#else
SendMessage (hEdit, EM_SETSEL, 0, MAKELONG (ndx, ndx));
#endif
SendMessage (hEdit, EM_REPLACESEL, 0, (LPARAM) ((LPSTR) szBuffer));
Another way to insert text into an edit control is to use the Windows
clipboard. If the application has the clipboard open or finds it convenient
to open the clipboard, and copies the text into the clipboard, then it can
send the WM_PASTE message to the edit control to append text. Of course,
any data that was in the clipboard will be lost.
OpenClipBoard () ;
EmptyClipBoard() ;
SetClipBoardData() ;
#ifdef WIN32
SendMessage (hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);
#else
SendMessage (hEdit, EM_SETSEL, 0, MAKELONG (ndx, ndx));
#endif
SendMessage (hEdit, WM_PASTE, 0, 0L);
This "pseudo" code appends text to the end of the edit control. Note that
the data in the clipboard must be in CF_TEXT format.
Keywords: kbhowto kbeditctrl kbctrl KB109550