11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14. Views<br />

ID_EDIT_PASTE<br />

paste<br />

The reason for this is that CEditView already maps commands with the above-mentioned IDs to its<br />

built-in member functions that handle undo, cut, copy and paste commands. The name of these functions<br />

are not documented in the current version of Visual C++, this means these function are not guaranteed to be<br />

supported in the future.<br />

If we want to use other command IDs instead of the recommended ones, we need to implement<br />

command message mapping by ourselves. In order to do so, we need to look at the <strong>MFC</strong> source code that<br />

contains member functions of class CEditView, find out the function names that support these commands,<br />

and map the WM_COMMAND type messages to the appropriate functions.<br />

Search Related Commands<br />

In the sample, three other standard commands, Search | Find…, Search | Replace…, and Search |<br />

Find Next are implemented this way. The three commands are used for searching a specific string in the<br />

text contained in the view, replacing an old string with a new one, or repeating searching. The default IDs<br />

for these commands are listed in the following table:<br />

Command ID Functionality WM_COMMADN message handler UPDATE_COMMAND_UI message<br />

handler<br />

ID_EDIT_FIND Search OnEditFind OnUpdateNeedText<br />

ID_EDIT_REPLACE Find/replace OnEditReplace OnUpdateNeedText<br />

ID_EDIT_REPEAT Repeat searching OnEditRepeat OnUpdateNeedFind<br />

Message UPDATE_COMMAND_UI is also handled for the above commands.<br />

In the sample, we use following non-standard command IDs to implement find, replace, and repeat<br />

commands:<br />

Command ID<br />

ID_SEARCH_FIND<br />

ID_SEARCH_REPLACE<br />

ID_SEARCH_FINDNEXT<br />

Functionality<br />

Find<br />

Find and replace<br />

Repeat<br />

The message mapping is done in the implementation file “NPView.cpp”:<br />

ON_COMMAND(ID_SEARCH_FIND, CEditView::OnEditFind)<br />

ON_COMMAND(ID_SEARCH_REPLACE, CEditView::OnEditReplace)<br />

ON_COMMAND(ID_SEARCH_FINDNEXT, CEditView::OnEditRepeat)<br />

ON_UPDATE_COMMAND_UI(ID_SEARCH_FIND, CEditView::OnUpdateNeedText)<br />

ON_UPDATE_COMMAND_UI(ID_SEARCH_REPLACE, CEditView::OnUpdateNeedText)<br />

ON_UPDATE_COMMAND_UI(ID_SEARCH_FINDNEXT, CEditView::OnUpdateNeedFind)<br />

With the above implementation, there is no need for us to declare and define new member functions to<br />

handle the above commands, everything will be handled automatically.<br />

Other Commands<br />

In the sample, some other commands that are not supported by class CEditVew are also implemented.<br />

These command include Edit | Delete, which can be used to delete the current selection; Edit | Select All,<br />

which can be used to select all the text contained in the window, and Edit | Time/Date, which can be used<br />

to insert a time/date stamp at the current caret position. For these commands, the message mapping and<br />

message handlers need to be implemented by ourselves.<br />

Edit view is implemented by an embedded edit control, which can be accessed by calling function<br />

CEditView::GetEditCtrl(). Once this is done, we can call any member function of CEdit and make<br />

change to the text contained in the window. For example, if we want to replace the selected text with a new<br />

string or insert a string at the current caret position, we can call function CEdit::ReplaceSel(…) to do so.<br />

429

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!