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

We call ::GetCursorPos(…) and CWnd::ScreenToClient(…) to retrieve the current cursor position in<br />

the client window’s coordinate system. Then functions CGDIDoc::GetTextHorSize() and CGDIDoc::<br />

GetCaretVerSize() are called to retrieve the dimension of the text. If the mouse cursor is within this<br />

rectangle, we call ::SetCursor(…) to change it to insertion cursor. In this case, we must return a TRUE<br />

value to avoid this message from being further processed (by default the mouse cursor will be set to arrow<br />

cursor).<br />

Handling WM_LBUTTONDOWN to Move Caret<br />

The caret can be moved when the current mouse cursor is an insertion cursor. To implement this, we<br />

need to call function CGDIDoc::SetCaret(…) after receiving WM_LBUTTONDOWN message. In the sample, this<br />

message handler is added through using Class Wizard, and the corresponding function CGDIView::<br />

OnLButtonDown(…) is implemented as follows:<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 />

In this function, first we check if the mouse cursor is the insertion cursor. If not, it means that the<br />

mouse is not over the text string. If so, we call function CGDIDoc::SetCaret(…) and pass current mouse<br />

position to it. This will cause the caret to move to the new position.<br />

9.8 One Line Text Editor, Step 5: Selection<br />

Sample 9.8\GDI is based on sample 9.7\GDI.<br />

Highlighting the Selected Text<br />

The next feature we will add is to let the user select text using mouse. If this is implemented, it is easy<br />

for us to add cut, copy and paste functionalities.<br />

To add the selection feature, we need two new text indices: one indicates the beginning of the<br />

selection, one indicates the end of the selection. In the sample, two variables along with two functions are<br />

added to class CGDIView for this purpose:<br />

……<br />

……<br />

……<br />

class CGDIDoc : public CDocument<br />

{<br />

protected:<br />

int m_nTextHorSize;<br />

int m_nSelIndexBgn;<br />

int m_nSelIndexEnd;<br />

public:<br />

}<br />

int GetTextHorSize(){return m_nTextHorSize;}<br />

int GetSelIndexBgn(){return m_nSelIndexBgn;}<br />

int GetSelIndexEnd(){return m_nSelIndexEnd;}<br />

Here, variables m_nSelIndexBgn, m_nSelIndexEnd, functions CGDIDoc::GetSelIndexBgn() and<br />

CGDIDoc::GetSelIndexEnd() are added to the class. Before the selection is made, there may exist two<br />

situations: the text is currently being selected or there is no character being selected. To distiguish between<br />

the two situations, let’s define that if any of m_nSelIndexBgn and m_nselIndexEnd is -1, or their values are<br />

264

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

Saved successfully!

Ooh no, something went wrong!