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 />

Another feature of this editor is Edit | Paste Special… command. Because the editor supports not only<br />

text, but also graphic editing now, the paste command should support multiple-format data. If we execute<br />

command Edit | Paste Special..., a dialog box will pop up indicating the available data formats contained<br />

in the clipboard, from which we can make the selection. For example, if we paste a bitmap, we have the<br />

choice to paste it either as bitmap format, metafile format or DIB format.<br />

The third feature of this editor is font selection. We may want to format different portion of the text<br />

using a different font. This is the default feature of class CRichEditView. The command ID that can be used<br />

to format the selected text using a specific font is ID_FORMAT_FONT (class CRichEditView supports this ID).<br />

So all we need to do is adding a Format | Font… command to menu IDR_MAINFRAME. With this simple<br />

implementation, we are able to format the text with any font that is available in the system.<br />

Although it is very easy to build a fully functional application with a lot of enticing features, it is<br />

relatively difficult to make modifications. For example, the standard Wordpad application under<br />

Windows has a ruler and a format bar, if we want to add these features, we need to add them by<br />

ourselves.<br />

Customizing File Open Dialog Box<br />

Class CRichEditView and CRichEditDoc can handle not only rich text format, but also plain text<br />

format (or ASCII format). By default, the two classes interpret input data using rich text format, if the<br />

format is different, the file will not be loaded. To let the application also support plain text format, we need<br />

to include multiple document types in “File Open” and “Save As” file dialog boxes, this gives the user an<br />

option for specifying file type. Although we can register more than one type of document to implement<br />

this, in the case of rich edit view, it is not an efficient way. This is because both formats are already<br />

supported by class CRichEditDoc.<br />

To implement customized “File Open” dialog box, we can override function<br />

CWinApp::OnFileOpen(…). We need to provide “File Open” dialog box, let the user pick up a file name,<br />

and pass this name to function CWinApp::OpenDocumentFile(…). Here we need to pay special attention to<br />

file formats. Because we support more than one file format here, we need to implement a “File Open”<br />

dialog box supporting multiple file filters, and inform document the file format that was selected by the<br />

user. To customize a file open dialog box, we need the knowledge of Chapter 6; to let the document support<br />

a different file format, we can set a Boolean type member variable of class CRichEditDoc:<br />

CRichEditDoc::m_bRTF, which is a public member variable. If this variable is set to TRUE, the data in the<br />

file will be treated as formatted data stream; if it is set to FALSE, the data will be treated as unformatted<br />

data stream (plain ASCII text). We need to set this flag before function CWinApp::OpenDocumentFile(…) is<br />

called.<br />

The following code fragment shows how function CWordPadApp::OnFileOpen() is implemented in the<br />

sample:<br />

void CWordPadApp::OnFileOpen()<br />

{<br />

CFileDialog dlg(TRUE);<br />

CString szName;<br />

CRichEditDoc *pDoc;<br />

}<br />

dlg.m_ofn.lpstrFilter=STRING_FILTER;<br />

dlg.m_ofn.nFilterIndex=1;<br />

if(dlg.DoModal() == IDOK)szName=dlg.GetPathName();<br />

pDoc=(CRichEditDoc *)(((CFrameWnd *)m_pMainWnd)->GetActiveDocument());<br />

if(dlg.m_ofn.nFilterIndex == 1)<br />

{<br />

pDoc->m_bRTF=TRUE;<br />

}<br />

else pDoc->m_bRTF=FALSE;<br />

OpenDocumentFile(szName);<br />

We must map command ID_FILE_OPEN to this function in order to make it effective. In the sample,<br />

WM_COMMAND message mapping for this command is customized as follows:<br />

431

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

Saved successfully!

Ooh no, something went wrong!