Some problems encountered by C + + MFC simple text Editor

Source: Internet
Author: User
Tags save file

"topic + "Simple text editor.

Design a simple text editor.

Design Requirements:

(1) A graphical menu interface;

(2) Find, replace (equal length, unequal length), insert (insert string, text block insertion), text block Move (row block, column block move), delete;

(3) can be correctly filed, take the disk;

(4) The total number of rows is displayed correctly.

This is the title .......... Using the dialog-based MFC I think of the point of feeling problematic! Find replacement algorithm do not understand the most annoying when the text block mobile Baidu Many industries can not find!! Oh uncomfortable is in dear hair a two posts to ask for ideas by the dog scold that person is true Divine character attached link https://www.52pojie.cn/forum.php?mod=viewthread&tid=792411& page=1#pid21689077 Everybody know about it (this kind of person in reality I want to see his skull hard or my brick hard, read him a comment he did not understand C, key foot pack big tail Wolf daily spray, but not Lao bar post not allow comments can tmbb a year, really enough to give him the face of it)

Text block move use RichEdit control this is a self-brought text block mobile Access disk problem can read and write files (here may encounter coding problems Chinese garbled!!!) ) code I will append the link https://www.lanzous.com/i1u3ojc (VS2013 compiled by) GitHub will not use to forgive me

First of all, the RichEdit needs to be initialized not clear words to take Baidu to use no one will not be able to run (flash back) in your InitInstance () add AfxInitRichEdit2 ();//Initialize edit box

And then it's the one that finds the replacement. Need to register a message it should be like this. C+mfc learned a little.

Add in your DLG.CPP header (why no other (not dlg.cpp) file has not been called I don't know) it happened. static const UINT Msgfindreplace =:: RegisterWindowMessage ( findmsgstring); This section lacks that Begin_message_map (Cjiandanbianjiqidlg, CDialogEx) on_wm_paint () On_wm_querydragicon () ON_ Wm_size () on_en_update (idc_richedit21, &cjiandanbianjiqidlg::onenupdaterichedit21) ON_REGISTERED_MESSAGE ( Msgfindreplace, OnFindReplace) on_command (Id_tihuanchazhao, &cjiandanbianjiqidlg::ontihuanchazhao) ON_COMMAND ( Id_dakai, &cjiandanbianjiqidlg::ondakai) on_command (Id_lingcunwei, &cjiandanbianjiqidlg::onlingcunwei) END _message_map () Replication OnFindReplace (WPARAM WPARAM, LPARAM LPARAM) method The following is the code on the Internet a lot of Baidu do not know that the home If the person sees this code, contact the complement reference link this function remembers to define a long cjiandanbianjiqidlg::onfindreplace in the header file (WPARAM WPARAM, LPARAM LPARAM) {int curpos = 0; int pos = 0;if (pfindreplacedlg->isterminating ()) {//Determines whether the dialog box is closed Pfindreplacedlg = NULL;} Gets the pointer to the current Find dialog box CString strText, Reptext; CFindReplaceDialog * Pdlg = Cfindreplacedialog::getnotifier (lParam); strText = Pdlg->getfindstring (); To findThe string CString str; The total string m_richedit.getwindowtext (str) in the RichEdit control; bianjikuang1. GetWindowText (str); int len = Strtext.getlength (); String length to find long index = 0, End_index = 0; The starting and ending position of the index, starting with 0 if (find)//Find dialog box if (Pdlg->searchdown ()) {bianjikuang1. GetSel (index, end_index); index = str. Find (StrText, End_index); At the end of the last query, continue to query the IF (index!=-1)//down search, find the substring {bianjikuang1) you want to query. SetSel (index, index + len); Sets the selection tag bianjikuang1 for the found substring. SetFocus (); On the substring found, set focus}else{index = str. Find (strText, 0); From the beginning of the file, re-find, you can implement when the search to the end of the file, the point to find the Next button, you can re-check from the beginning, so that the loop function if (index = =-1) {MessageBox (L "To find complete, no content");//If you find it from the beginning, , then none of this finds return 0;} Bianjikuang1. SetSel (index, index + len); If you look up from the beginning and query to the content you are looking for, mark it bianjikuang1. SetFocus ();}} Else{bianjikuang1.                    GetSel (index, End_index); Gets the beginning and ending position of the currently selected text CString StrReverse = str.                  Makereverse ();          Reverses the text in the control CString strtextreverse = Strtext.makereverse (); Reverses the text of the lookup string index = Strreverse.find (Strtextreverse, str.    GetLength ()-index); Look down from crossdress to find the start position index of ID=STR. The previous bit of length-index-1, which is the previous bit from the beginning of the source string, cannot be searched from the beginning of the source string, because if only a single character is always queried for that position, no further queries are made to the IF (Index! =-1) {end_index = str).   GetLength ()-index-1; The source string and crossdress index}else{index = strreverse.find (strtextreverse, 0), if (index = =-1) {MessageBox (L "to find finished, no content found");} Bianjikuang1. SetSel (index, index + len); bianjikuang1. SetFocus ();}} if (Pdlg->replacecurrent ())//Replace {if (index = =-1) {MessageBox (L "To find complete, no content found");} Else{bianjikuang1. GetSel (index, End_index); CString Sub_left, Sub_mid, sub_ringht; Sub_left = str. Left (index); Sub_mid = str. Mid (index, End_index-index), if (sub_mid! = StrText)//Determines whether the current selection text is find text {MessageBox (L "Please re-search"); return 0;} Sub_ringht = str. Right (str. GetLength ()-1-end_index + 1); The right string contains the character end_index this position reptext = pdlg->getreplacestring (); str = sub_left + Reptext + sub_ringht;bianjikuang1. SetWindowText (str);}} if (Pdlg->replaceall ()) {int num = 0; CString Mssag;reptext = pdlg->getreplacestring (); len = Reptext.getlength (); nUm = str. Replace (StrText, Reptext); bianjikuang1. SetWindowText (str); Mssag. Format (L "All replacements have been completed, total replacement%d", num); MessageBox (MSSAG);}}

The next problem is open save file encountered garbled problem Baidu has not zhi a

Open File

CFileDialog Dlg (True, _t ("txt"), _t ("*.txt"), Ofn_hidereadonly | Ofn_overwriteprompt, _t ("All Files (*. TXT) |*. txt| | "), NULL); CString strpath, Strtext;if (dlg. DoModal () = = IDOK) {strpath = dlg. GetPathName ();//m_op. SetWindowText (strpath); CFile file (strpath, cfile::moderead); tchar* temp = new Tchar[file. GetLength ()/2 + 1];file. Read (temp, file. GetLength ()); Temp[file. GetLength ()/2] = 0;file. Close (); bianjikuang1. SETWINDOWTEXTW (temp);}

Save File

CFileDialog dlg (FALSE); CString strpath, Strtext;char write[10000];if (dlg. DoModal () = = IDOK) {strpath = dlg. GetPathName (); if (Strpath.right (4)! = ". TXT ") strpath + =". TXT ";//setdlgitemtext (IDC, strpath); CFile file (strpath, Cfile::modecreate | Cfile::modewrite); GetDlgItemText (idc_richedit21, StrText); CStringA stra (strtext.getbuffer (0)); Strtext.releasebuffer (); std::string s = stra.getbuffer (0); const char* Tempya = S.C _str (); strcpy_s (write, tempya); WORD Unicode = 0xFEFF;  This is important, notice here is F E FF, there is a efile.write in the middle (&unicode, 2);  This sentence is important file. Write (StrText, Wcslen (strText) *sizeof (wchar_t)); file. Close ();}

The code link has been attached!!! Status bar I will not say do not understand the specific Baidu!

Some problems encountered by C + + MFC simple text Editor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.