http://blog.csdn.net/cnki_ok/article/details/7388150
"Originally did a dialog box implementation of the text editor, in fact, with the MFC template inside the single document template can also be done, or even more convenient, suitable for entry-level enthusiasts to try, now introduce the method as follows:
1, first create a new project, select Mfcappwizard (EXE), the name is: Textview_1, the program type select a single document, others are completed by default.
2, add a control to the system automatically generated Ctextview_1doc class for temporary storage of text documents:
Class Ctextview_1doc:public CDocument
{
......
Public
CStringArray m_strcontent;
}
Then add the program to open and save the text file in the Serialize function of the Ctextview_1doc class:
void Ctextview_1doc::serialize (carchive& ar)
{
CString str;
if (AR. IsStoring ())
{
Todo:add Storing code here
int nlines = (int) m_strcontent.getsize ();
for (int i=0; i<nlines; i++)
{
str = M_strcontent.getat (i);
Ar. WriteString (str); To save text from a String collection class object to a hard disk
}
}
Else
{
Todo:add Loading code here
while (AR. ReadString (str))
{
M_strcontent.add (str); Add line text to a String collection class object
}
}
}
Then the mouse on the Ctextview_1doc right click, in the Pop-up menu select: Add Virtual Function, in the pop-up window selected DeleteContents, click the "Add and edit" button. Add the following code to the generated program:
void Ctextview_1doc::D eletecontents ()
{
Todo:add your specialized code here and/or call the base class
M_strcontent.removeall (); Clears the contents of the collection class object
CDocument::D eletecontents ();
}
3, add an editor's control pointer member to the system's automatically generated Ctextview_1view class to generate a text editor in the interface:
Class Ctextview_1view:public CView
{
......
Public
cedit* M_ctrledit;
}
The pointer member will set the initialization value to NULL when the class is established, or else run up with an error, as follows:
Ctextview_1view::ctextview_1view ()
: M_ctrledit (NULL)//Add this line of initialization code
{
Todo:add Construction Code here
}
To rewrite its oninitialupdate function for the Ctextview_1view class with the similar steps described in the 2nd, add the following:
void Ctextview_1view::oninitialupdate ()
{
CView::OnInitialUpdate ();
Todo:add your specialized code here and/or call the base class
CRect rcclient;
GetClientRect (rcclient); Gets the client area size of the current view
if (m_ctrledit) Delete m_ctrledit;
M_ctrledit = new CEdit ();
M_ctrledit->create (Es_multiline | Ws_child | Ws_visible
| Ws_hscroll | Es_autohscroll//Automatic horizontal scrolling
| Ws_vscroll | Es_autovscroll,//automatic vertical scrolling
Rcclient, this, 201); To create a multi-line edit control
ctextview_1doc* PDoc = GetDocument (); Gets the document pointer associated with the view
The following is a full assignment of the M_strcontent content in the document to Str
CString str;
int nlines = (int) pdoc->m_strcontent.getsize ();
for (int i=0; i<nlines; i++)
{
str = str + pdoc->m_strcontent.getat (i);
str = str + "\ r \ n"; Line break
}
M_ctrledit->settabstops (16); Set Tab character size
M_ctrledit->setwindowtext (str); Passing the contents of a document to a control
}
4, now run, the text editor is ready.
5, of course, now the text editor is very bad, because the window cannot be scrolled. And it doesn't change with the size of the window, so you need to add some code. Mouse over the Ctextview_1view right-click, in the Pop-up menu select: Add windowmessage Handle, in the pop-up window, select the Wm_size message, and click the "Add & Edit" button, In the generated function, add the following:
void Ctextview_1view::onsize (UINT nType, int cx, intcy)
{
Cview::onsize (NType, CX, CY);
Todo:add your message Handler code here
CRect rcclient;
GetClientRect (rcclient);
if (M_ctrledit)
M_ctrledit->movewindow (rcclient); Change the size of the Edit Control window
}
6, run again, an image-like editor generated.