Inserting text in CEditView

I created an editor with the CEditView MFC. I added a dialogbox where I can enter one line of text. When I press on OK, I want the text to be pasted in my document. How can I write to my editor document,or is there a way to write to the clipboard so that I can paste the text from the clipboard?
Who is Participating?
Answer in my comment below, takes me a while to type <g>
1. To insert text on the clipboard use the following

Crude code (you should check return values) :-

// say char szBuffer[] contains the text to put on the clipboard

// allocate HGLOBAL memory for the text (extra char is the terminating NULL)
HGLOBAL hText = GlobalAlloc( GHND, strlen(szBuffer)+1 ) ;
LPSTR lpText = (LPSTR)GlobalLock( hText ) ;
memcpy( lpText, szBuffer, strlen(szBuffer)+1 ) ;
GlobalUnlock( hText ) ;

// put new text in clipboard
::OpenClipboard( NULL ) ; // open clipboard for current task
::EmptyClipboard() ;
::SetClipboardData( CF_TEXT, hText ) ;
::CloseClipboard() ;

2. You can force pasting of text into the edit control using the WM_PASTE message, for each in your class derived from CEditView

GetEditCtrl().SendMessage( WM_PASTE, 0, 0 ) ;

3. If you just want to manipulate the text in the edit control (and don't care about the clipboard)

i. use GetWindowText to get the text out of the window.  This gives you a CString
ii. Manipulate the CString as necessary
iii. use SetWindowText to put the text back into CEditView

CString strText = GetEditCtrl().GetWindowText() ;
strText += "another word" ;
GetEditCtrl().SetWindowText( strText ) ;

You can find the text selection of a CEditView using GetSelectedText, and use CEdit member functions to find out any other info you want.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.