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

if(m_nSelIndexBgn == -1)m_nSelIndexBgn=m_nCaretIndex;<br />

else<br />

{<br />

m_nSelIndexEnd=m_nCaretIndex;<br />

UpdateAllViews(NULL);<br />

}<br />

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

We want to use this function to set both m_nSelIndexBgn and m_nSelIndexEnd, so that it can be called<br />

in response to any of the three mouse messages. If the value of m_nSelIndexBgn is -1, it means there is no<br />

currently selected text. In this case, we need to update m_nSelIndexBgn (This is the situation that the left<br />

button of the mouse is pressed down). In other cases (If m_nSelIndexBgn is not -1, the function must be<br />

called in response to mouse moving or left button up event), we need to update the value of<br />

m_nSelIndexEnd, then update the client window.<br />

Handling Mouse Events<br />

First function CGDIView::OnLButtonDown(…) is modified as follows:<br />

Old Version:<br />

void CGDIView::OnLButtonDown(UINT nFlags, CPoint point)<br />

{<br />

CGDIDoc *ptrDoc;<br />

}<br />

ptrDoc=(CGDIDoc *)GetDocument();<br />

if(::GetCursor() == m_hCur)ptrDoc->SetCaret(point);<br />

CScrollView::OnLButtonDown(nFlags, point);<br />

New Version:<br />

void CGDIView::OnLButtonDown(UINT nFlags, CPoint point)<br />

{<br />

CGDIDoc *ptrDoc;<br />

}<br />

ptrDoc=(CGDIDoc *)GetDocument();<br />

ptrDoc->ResetSelection();<br />

if(::GetCursor() == m_hCur)ptrDoc->SetCaret(point);<br />

CScrollView::OnLButtonDown(nFlags, point);<br />

The only thing added to this function is resetting the selection indices stored in the document. Other<br />

changes are implemented in the updated function CGDIView::SetCaret(…).<br />

Two other message handlers for WM_LBUTTONUP and WM_MOUSEMOVE are added through using Class<br />

Wizard. The corresponding functions are CGDIView::OnLButtonUp(…) and CGDIView::OnMouseMove(…)<br />

respectively.<br />

Function CGDIView::OnMouseMove(…) is implemented as follows:<br />

void CGDIView::OnMouseMove(UINT nFlags, CPoint point)<br />

{<br />

CGDIDoc *ptrDoc;<br />

}<br />

if(nFlags & MK_LBUTTON)<br />

{<br />

ptrDoc=(CGDIDoc *)GetDocument();<br />

if(::GetCursor() == m_hCur)ptrDoc->SetCaret(point);<br />

}<br />

CScrollView::OnMouseMove(nFlags, point);<br />

267

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

Saved successfully!

Ooh no, something went wrong!