MFC Implements Notepad functionality

Source: Internet
Author: User
Notepad's basic functions have all been implemented: using the CEdit class implementation, including opening files, saving files, pasting, copying and cutting, etc .:

 

1:

// Implement the copy function under the menu column
void CNotePadDlg :: OnEditCopy ()
{
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
    pEdit-> Copy ();
}

// Implement the deleted function under the menu column
void CNotePadDlg :: OnEditClear ()
{
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> Clear ();
}
// implement the undo function under the menu column
void CNotePadDlg :: OnEditUndo ()
{
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> Undo ();
}

// Achieve the cut function under the menu bar
void CNotePadDlg :: OnEditCut ()
{
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> Cut ();
}
// Realize the function of selecting all under the menu column
void CNotePadDlg :: OnEditSelectAll ()
{
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> SetSel (0, -1);
}
// Realize the paste function under the menu column
void CNotePadDlg :: OnEditPaste ()
{
Ranch
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> Paste ();
}
// Realize the time function under the menu column
void CNotePadDlg :: OnEditDate ()
{
CTime t = CTime :: GetCurrentTime ();

CString str = t.Format ("% Y year% m month% d day% H:% M:% S");
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> ReplaceSel (str, TRUE);
}

2:

// Implement the function of moving the window size; remember to modify the values in the window properties
void CNotePadDlg :: OnSize (UINT nType, int cx, int cy)
{
CDialog :: OnSize (nType, cx, cy);
Ranch
CWnd * pEdit = GetDlgItem (IDC_TEXT);
if (pEdit)
pEdit-> MoveWindow (0,0, cx, cy);
Ranch
}

// Implement the exit function under the menu
void CNotePadDlg :: OnFileExit ()
{
if (Prompt () == 0) // If it is equal to 0, it means cancel is selected, then the dialog box is not closed.
{
// do nothing
}
else // If it is not equal to 0, then it means that no is selected. Close the dialog box directly.
{
EndDialog (IDCANCEL);
}
Ranch
}

3:

// Implement the display of the dialog box
void CNotePadDlg :: OnHelpAbout ()
{
CAbout dlg;
dlg.DoModal ();
Ranch
}

// Disable esc to exit the program
void CNotePadDlg :: OnCancel ()
{
// CDialog :: OnCancel ();
}

// Overload OnClose () to implement X's exit function
void CNotePadDlg :: OnClose ()
{
OnFileExit (); // Call the exit function under the menu here
// EndDialog (IDCANCEL);
// CDialog :: OnClose ();
}

4:

// Realize the function of save as under the menu column
void CNotePadDlg :: OnFileSaveAs ()
{
Ranch
LPCTSTR szFilter = "Text file (* .txt) | * .txt | All files (*. *) | *. * ||";
// Initialize file dialog FALSE stands for save file
CFileDialog filedlg (FALSE, "txt", "11", OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST |
OFN_FILEMUSTEXIST | OFN_CREATEPROMPT, szFilter);
// Open file dialog
if (IDCANCEL == filedlg.DoModal ())
return;
// Get the path name of the selected file in the file dialog
CString szFile = filedlg.GetPathName ();

// Declaring a CFile object
CFile file;
//open a file
if (file.Open (szFile, CFile :: modeWrite | CFile :: modeCreate) == 0)
{
MessageBox (filedlg.GetFileName () + "Failed to save file!", NULL, MB_ICONEXCLAMATION);
return;
}

// The current file is this file, save it
m_szCurrentFile = szFile;

CString strText;
// Get the contents of the IDC_TEXT control
GetDlgItemText (IDC_TEXT, strText);
// File write, the first parameter is the content to be written, and the second is the length of the content to be written
file.Write (strText, strText.GetLength ());

/ *
1: Flush is to write the contents of the buffer to a file and empty it. The buffer is 4k content, 4k is enough,
Will automatically write the file, if not enough, you need to flush it, if not, the file
May end up with less content
2: Flush () flushes the buffer. If you call Close () after write () is completed,
That part of the data in the buffer is lost, because the data is first written into memory and then read into the file.
So force the data in the buffer before calling Close () so that no data is lost.
3: The function of flush () is to force the data in the buffer to be written to disk, just to ensure that the data is written as soon as possible, but in fact, if you use flush () for a small amount of data, the mechanical operation time of the hard disk write is relatively comparative long,
If you do not call flush (), the buffer will automatically write to disk when it reaches a certain amount of data.
When the file is closed, the buffer data (if any) is also forcibly written to disk
4: If you do not write the same file in multiple threads, you don't need to flush (),
Remember to close before the end, of course, you can also flush the last one, and then close
* /
file.Flush (); // The explanation above is cleared, I see this to understand, I believe you are the same

file.Close (); // Close the file and delete the object!

CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> SetModify (FALSE);
SetTitle ();
Ranch
}

5:

// Realize the function opened under the menu column
void CNotePadDlg :: OnFileOpen ()
{
//filter
LPCTSTR szFilter = "Text file (* .txt) | * .txt | All files (*. *) | *. * ||";
// Open a file selection dialog box
CFileDialog filedlg (TRUE, "txt", "11", OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST,
szFilter);
if (IDCANCEL == filedlg.DoModal ())
return;
// Open the file and display it in IDC_TEXT
OpenFile (filedlg.GetPathName (), filedlg.GetFileName ());
}

void CNotePadDlg :: OpenFile (CString szFilePath, CString szFileName)
{

CFile file; // Declares a CFile object

// Open a file
if (file.Open (szFilePath, CFile :: modeRead) == 0)
{
MessageBox (szFileName + "Failed to open file!", NULL, MB_ICONEXCLAMATION);
return;
}
/ *
int size = file.GetLength (); // Get the length of the file
char * pText = new char [size + 1]; // A pointer to read into the file buffer

// Read the data of the file associated with the CFile object file into the buffer.
int nRet = file.Read (pText, size);
file.Close (); // Close the object
* /

//The second method
CFileStatus fs;
file.GetStatus (fs);
char * pText = new char [fs.m_size + 1];
memset (pText, 0, fs.m_size + 1);
int nRet = file.Read (pText, fs.m_size);
file.Close ();
/ *
Explain here that your file may be garbled, that is because the file you want to open is unicode or other format that is not ANSI
What is the format of the file you want to open?
Open the file you want to open with Notepad, and then save as, you will see what format your file is in. When you change it to ANSI format
There will be no garbled characters!
* /
pText [nRet] = 0; // Set the end to 0 and end the string with \ 0;
m_szCurrentFile = szFilePath; // Set the current file path to the currently opened file path

CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> SetWindowText (pText);
pEdit-> SetModify (FALSE);

SetTitle ();

}

6:

// Realize the function saved under the menu column
void CNotePadDlg :: OnFileSave ()
{
// If the file to be saved is saved for the first time, then switch to Save As
if (m_szCurrentFile.GetLength () == 0)
{
OnFileSaveAs ();
return;
}
Ranch
CFile file;
if (file.Open (m_szCurrentFile, CFile :: modeWrite | CFile :: modeCreate) == 0)
{
OnFileSaveAs ();
return;
Ranch
}

CString szText;
GetDlgItemText (IDC_TEXT, szText); // Get the contents of the Edit control
file.Write (szText, szText.GetLength ()); // Write to a file object
file.Close (); // Close the object
Ranch
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> SetModify (FALSE); // FALSE means that the text has not changed; save it directly here, don't prompt for changes when you click to exit

}

7:

// Realize the new function under the menu bar
void CNotePadDlg :: OnFileNew ()
{
// Handle the change of the file; if we change the original file when we create a new file
// If == 0; it means cancel, then return directly, which means cancel the newly created task

//in case! = 0; then it is possible to save the current text 

Files or do not save the current file
if (Prompt () == 0)
return;

// The following is the task of creating a new file.
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
pEdit-> SetWindowText ("");
m_szCurrentFile = "";
SetTitle ();

Ranch
}

8:

// Handle file modification
BOOL CNotePadDlg :: Prompt ()
{
CEdit * pEdit = (CEdit *) GetDlgItem (IDC_TEXT);
if (pEdit-> GetModify ()) // If the file is modified
{
CString str;
if (m_szCurrentFile.IsEmpty ()) // If the current file is empty, str = no title
str = "Untitled";
else
str = m_szCurrentFile; // Otherwise, the path to open the file now

int nRet = MessageBox (str + "\ r \ nhas been modified, save it?", "Prompt message",
MB_YESNOCANCEL | MB_ICONEXCLAMATION);

if (IDCANCEL == nRet) // If Cancel is selected, no processing is done and the dialog box is not closed (do processing in the exit function)
return FALSE;
if (IDNO == nRet) // If you select No, close the dialog box directly (do it in the exit function)
return TRUE;
else // if selected yes save file
 OnFileSave ();
}

return TRUE;
}

// Set the title above
void CNotePadDlg :: SetTitle ()
{
CString str;
if (m_szCurrentFile.IsEmpty ())
str = "Untitled";
else
str = m_szCurrentFile;

int i = str.ReverseFind ('\\'); // Reverse find \ if (i> 0)
str = str.Mid (i + 1); // Truncate the content after str from \\

SetWindowText (str);
}

9:

// Implement the function of font selection under the menu column
void CNotePadDlg :: OnFormatFont ()
{
Ranch
CFont * pFont = GetDlgItem (IDC_TEXT)-> GetFont ();
LOGFONT lf;
pFont-> GetLogFont (& lf); // Get a copy of the LOGFONT structure of the current CFont object
CFontDialog fd (& lf); // Construct the font dialog, the initial selection font is the font of the current CFont object

if (IDCANCEL == fd.DoModal ())
return;
m_font.DeleteObject (); // If m_font is already associated with a font resource object, release it
m_font.CreateFontIndirect (fd.m_cf.lpLogFont); // Create a new font using LOGFONT of the selected font
// m_font.CreateFontIndirect (& lf);
GetDlgItem (IDC_TEXT)-> SetFont (& m_font); // Select font
Ranch
}

 

10:

// Implement the function of dragging a file into
void CNotePadDlg :: OnDropFiles (HDROP hDropInfo)
{
if (! Prompt ())
return;
char szFile [MAX_PATH];
UINT n = DragQueryFile (hDropInfo, 0, szFile, sizeof (szFile));
CString str = szFile;
n = str.ReverseFind ('\\');
if (n> 0)
{
str = str.Mid (n + 1);
OpenFile (szFile, str);
}

CDialog :: OnDropFiles (hDropInfo);
}

Related Article

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.