11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9. Font<br />

}<br />

hMem=::GlobalAlloc(GHND | GMEM_SHARE, szStr.GetLength());<br />

ASSERT(hMem != NULL);<br />

lpStr=(LPSTR)::GlobalLock(hMem);<br />

ASSERT(lpStr != NULL);<br />

memcpy(lpStr, szStr, szStr.GetLength());<br />

::GlobalUnlock(hMem);<br />

GetCGDIView()->OpenClipboard();<br />

::EmptyClipboard();<br />

::SetClipboardData(CF_TEXT, hMem);<br />

::CloseClipboard();<br />

First, we assign the smaller of the two selection indicies to variable nSel, and the number of selected<br />

characters to variable nNum. Then we copy the selected text to a CString type variable szStr. Next, we<br />

allocate a memory block, lock it, copy the string from szStr to the new buffers. Then we unlock the<br />

memory, open the clipboard, clear it, and copy the data to the clipboard. Finally we close the clipboard.<br />

The implmentation of Edit | Cut command is almost the same except that we must delete the selected<br />

text after copying the data to the clipboard. So function CGDIDoc::OnEditCut() is implemented as follows:<br />

void CGDIDoc::OnEditCut()<br />

{<br />

OnEditCopy();<br />

DeleteSelection();<br />

}<br />

For Edit | Paste command, everything is the reverse. We need to open the clipboard, obtain data from<br />

the clipboard, lock the global memory, copy the data to local buffers, unlock the global memory, and insert<br />

the new string to the text at the current caret position. The following is the implementation of this<br />

command:<br />

void CGDIDoc::OnEditPaste()<br />

{<br />

HANDLE hMem;<br />

LPSTR lpStr;<br />

CString szStr;<br />

}<br />

DeleteSelection();<br />

GetCGDIView()->OpenClipboard();<br />

hMem=::GetClipboardData(CF_TEXT);<br />

ASSERT(hMem != NULL);<br />

lpStr=(LPSTR)::GlobalLock(hMem);<br />

ASSERT(lpStr);<br />

szStr=lpStr;<br />

m_nCaretIndex+=szStr.GetLength();<br />

::GlobalUnlock(hMem);<br />

::CloseClipboard();<br />

m_szText=m_szText.Left(m_nCaretIndex)+szStr+m_szText.Mid(m_nCaretIndex);<br />

UpdateAllViews(NULL);<br />

GetCGDIView()->RedrawCaret();<br />

Now the application can exchange data with another application that supports clipboard.<br />

9.10 One Line Text Editor, Step 7: Getting Rid of Flickering<br />

The editor is almost finished except for one annoying feature: every time the user inputs a character,<br />

makes selection or moves the caret, the text will flicker. This is because whenever the text is being updated,<br />

we call function CDocument::OnUpdateAllViews(…) to cause the whose view window to be updated. By<br />

default, before the client window is redrawn, it will be erased using white color. This is the cause of<br />

flickering. To get rid of it, we need to update only the area that has changed (instead of updating the whole<br />

window).<br />

272

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

Saved successfully!

Ooh no, something went wrong!