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

Function CDocument::UpdateAllViews(…)<br />

Function CDocument::UpdateAllViews(…) has three parameters, two of which have default values:<br />

void CDocument::UpdateAllViews(CView *pSender, LPARAM lHint=0L, CObject *pHint=NULL);<br />

By default, the update message will be sent to view, this will cause function CView::OnUpdate(…) to<br />

be called:<br />

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

All parameters passed to CDocument::UpdateAllViews(…) will be passed to this function. This<br />

provides us a way to know what part of the client window needs to be updated. The updating hint can be<br />

passed through either parameter lHint or pHint.<br />

By default, CView::OnUpdate(…) will update the whole client area. If we want only a portion of the<br />

client window to be updated, we need to bypass the default implementation. Within the overridden<br />

funciton, we can use the hint to form a rectangle indicating the area needs to be updated, and use it to call<br />

function CWnd::InvalidateRect(…).<br />

Function CWnd::InvalidateRect(…) will cause only the specified rectangular area to be updated.<br />

Defining Hints<br />

Our next task is to divide the updating events into different categories and calculate the rectangle for<br />

each situation. The following is a list of situations when only a portion of the client window needs to be<br />

updated:<br />

Activities<br />

Delete a character using DELETE key<br />

Paste<br />

Input a character<br />

Delete a character using BACK SPACE<br />

key<br />

Delete selection<br />

Select a portion of the text<br />

Unselect the text<br />

Area needs to be updated<br />

From the current caret to the end of text<br />

Same as above<br />

Same as above<br />

From the character before the caret to the end of text<br />

From the beginning of the selection to the end of text<br />

The difference between the newly selected area and previously<br />

selected area<br />

The whole selected area<br />

The last two situations are a little complicated. When the user makes selections, the newly selected<br />

area may be smaller or larger than the old selected area. In either case, we only need to update the changed<br />

area to avoid flickering (Figure 9-5).<br />

Because of this, we need to add new variables to remember the old selection indices. In the sample,<br />

two new variables and some functions are declared in class CGDIDoc as follows:<br />

……<br />

……<br />

……<br />

class CGDIDoc : public CDocument<br />

{<br />

protected:<br />

int m_nSelIndexBgnOld;<br />

int m_nSelIndexEndOld;<br />

public:<br />

}<br />

int GetSelIndexBgnOld(){return m_nSelIndexBgnOld;}<br />

int GetSelIndexEndOld(){return m_nSelIndexEndOld;}<br />

int GetCaretIndex(){return m_nCaretIndex;}<br />

273

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

Saved successfully!

Ooh no, something went wrong!