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

In this function, WPARAM parameter is mapped to nChar parameter. If the key stroke is from left arrow<br />

key, we call CGDIDoc::BackwardCaret() to move the caret leftward. If the key stroke is from right arrow<br />

key, we call CGDIDoc::ForwardCaret() to move the caret rightward.<br />

9.6 One Line Text Editor, Step 3: Enabling Input<br />

Sample 9.6\GDI is based on sample 9.5\GDI, it allows the user to input characters.<br />

New Member Functions<br />

We need to trap keyboard stroking events in order to let the user input characters. Since our data is<br />

stored in the document, we need to first provide some member functions that can be called from the view to<br />

let the new characters be added. For this purpose, two new functions are declared in class CGDIDoc:<br />

……<br />

……<br />

class CGDIDoc : public CDocument<br />

{<br />

}<br />

CGDIView *CGDIDoc::GetCGDIView();<br />

void AddChar(UINT, UINT);<br />

void DeleteChar(BOOL);<br />

//{{AFX_VIRTUAL(CGDIDoc)<br />

Function CGDIDoc::AddChar(…) allows us to insert characters to the string at the position indicated by<br />

the caret, and function CGDIDoc::DeleteChar(…) allows us to delete the character before or after the caret.<br />

Let’s first take a look at the implementation of function CGDIDoc::AddChar(…):<br />

void CGDIDoc::AddChar(UINT uChar, UINT uRepCnt)<br />

{<br />

CString szStr;<br />

UINT i;<br />

}<br />

szStr=m_szText;<br />

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

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

for(i=0; i

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

Saved successfully!

Ooh no, something went wrong!