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

{<br />

}<br />

UpdateAllViews(NULL, HINT_PASTE);<br />

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

Flag HINT_PASTE will cause all the characters after the caret to be updated.<br />

The following shows the modifications made to inline function CGDIDoc::ResetSelection():<br />

Old Version:<br />

……<br />

……<br />

class CGDIDoc : public CDocument<br />

{<br />

}<br />

void ResetSelection()<br />

{<br />

m_nSelIndexBgn=m_nSelIndexEnd=-1;<br />

UpdateAllViews(NULL);<br />

}<br />

New Version:<br />

……<br />

……<br />

class CGDIDoc : public CDocument<br />

{<br />

}<br />

void ResetSelection()<br />

{<br />

m_nSelIndexBgnOld=m_nSelIndexBgn;<br />

m_nSelIndexEndOld=m_nSelIndexEnd;<br />

m_nSelIndexBgn=m_nSelIndexEnd=-1;<br />

UpdateAllViews(NULL, HINT_UNSELECTION);<br />

m_nSelIndexBgnOld=m_nSelIndexEndOld=-1;<br />

}<br />

Flag HINT_UNSELECTION will cause only the selected area to be updated. Because both m_nSelIndexBgn<br />

and m_nSelIndexEnd should be set to -1 to indicate that there is no selected text anymore, we need to use<br />

two other variables (m_nSelIndexBgnOld and m_nSelIndexEndOld) to store the old selection indices.<br />

Overriding CView::OnUpdate(…)<br />

On the view side, function OnUpdate(…) can be added through using Class Wizard. In this function, we<br />

need to know the current values of CGDIDoc::m_nSelIndexBgn, CGDIDoc::m_nSelIndexEnd, CGDIDoc::<br />

m_nSelIndexBgnOld, CGDIDoc::m_nSelIndexEndOld and CGDIDoc::m_nCaretIndex in order to decide<br />

which part of the text should be updated. We also need to obtain the current font and text string in order to<br />

calculate the actual rectangle for implementing update.<br />

If parameter lHint is NULL, it means that all client area needs to be updated. In this case, we call the<br />

default implementation of this function and return. The following is a portion of funciton CGDIView::<br />

OnUpdate(…) which implements this:<br />

void CGDIView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)<br />

{<br />

CClientDC dc(this);<br />

CFont *ptrFontOld;<br />

CRect rect;<br />

CGDIDoc *ptrDoc;<br />

CString szText;<br />

CSize sizeExtent;<br />

int nIndex;<br />

int nSelIndexBgn;<br />

int nSelIndexEnd;<br />

277

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

Saved successfully!

Ooh no, something went wrong!