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 9. Font<br />

……<br />

}<br />

{<br />

Since this member function may be called when either BACK SPACE or DELETE key is pressed, we<br />

need to delete the selected text in both cases. If deleting the selected text is successful, the function will<br />

return. Otherwise it means there is no currently selected text, so we go on to delete a single character.<br />

Message Handlers for Cut, Copy Paste Commands<br />

In the sample, both WM_COMMAND and UPDATE_COMMAND_UI message hanlders are added for command<br />

ID_EDIT_CUT, ID_EDIT_COPY and ID_EDIT_PASTE in class CGDIDoc. We need to enable commands Edit |<br />

Copy and Edit | Cut if there is selected text. So functions CGDIDoc::OnUpdateEditCopy(...) and<br />

CGDIDoc::OnUpdateEditCut(...) are implemented as follows:<br />

void CGDIDoc::OnUpdateEditCopy(CCmdUI* pCmdUI)<br />

{<br />

BOOL bEnable=TRUE;<br />

}<br />

if(m_nSelIndexBgn == -1 || m_nSelIndexEnd == -1)bEnable=FALSE;<br />

if(m_nSelIndexBgn == m_nSelIndexEnd)bEnable=FALSE;<br />

pCmdUI->Enable(bEnable);<br />

void CGDIDoc::OnUpdateEditCut(CCmdUI* pCmdUI)<br />

{<br />

BOOL bEnable=TRUE;<br />

}<br />

if(m_nSelIndexBgn == -1 || m_nSelIndexEnd == -1)bEnable=FALSE;<br />

if(m_nSelIndexBgn == m_nSelIndexEnd)bEnable=FALSE;<br />

pCmdUI->Enable(bEnable);<br />

Two functions are implemented exactly the same. For function CGDIDoc::OnUpdateEditPaste(…), we<br />

need to check if there is data available in the clipboard, if so, the command will be enabled. This checking<br />

can be implemented by calling function ::IsClipboardFormatAvailable(…) with appropriate data format<br />

passed to it. The function will return FALSE if there is no data present in the clipboard for the specified<br />

data format. The following is the implementation of funcition CGDIDoc::OnUpdateEditPaste(…):<br />

void CGDIDoc::OnUpdateEditPaste(CCmdUI* pCmdUI)<br />

{<br />

pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));<br />

}<br />

Command Edit | Copy is implemented as follows:<br />

void CGDIDoc::OnEditCopy()<br />

{<br />

CString szStr;<br />

int nSel;<br />

int nNum;<br />

HANDLE hMem;<br />

LPSTR lpStr;<br />

if(m_nSelIndexEnd > m_nSelIndexBgn)<br />

{<br />

nSel=m_nSelIndexBgn;<br />

nNum=m_nSelIndexEnd-m_nSelIndexBgn;<br />

}<br />

else<br />

{<br />

nSel=m_nSelIndexEnd;<br />

nNum=m_nSelIndexBgn-m_nSelIndexEnd;<br />

}<br />

szStr=m_szText.Mid(nSel, nNum);<br />

271

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

Saved successfully!

Ooh no, something went wrong!