To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98
Chapter 5 Real-World Programming: The Editor Application
In this chapter, we examine several issues in a real-world application: a text editor. We see a lot about Visual C++ programming here. We learn how to:
- Work with files through the process of serialization
- Serialize our own objects
- Set the default extension given to an applications data files
- Register that extension with Windows so the user can open our application just by clicking a data file
- Change the default cursor type used in a view
- Enable Cut, Copy, and Paste operations in a program
- Use the clipboard to send data to and receive data from other applications
- Draw selected text
- Enable character erasure
- Print data
- Add headers to printed pages
As you can see, theres a great deal coming up in this chapter, so lets get started by creating our Editor program and enabling the program to accept character input.
Creating the Editor Program
The Editor program will let the user type in text.
------------------------------------------------------
| |
|------------------------------------------------------ |
| |
| -------------------------------- |
| |Here is some text. | |
| | | |
| | | |
| | | |
| | | |
| | | |
| -------------------------------- |
| |
| |
------------------------------------------------------
The user can also click the view in another location and type new text there.
------------------------------------------------------
| |
|------------------------------------------------------ |
| |
| -------------------------------- |
| | | |
| | | |
| | | |
| | Here is some new text. | |
| | | |
| | | |
| -------------------------------- |
| |
| |
------------------------------------------------------
The program can also write the text out to disk, read it back, select, cut, copy, paste text, and more. We start by enabling character input. Create a new MDI program, Editor, with AppWizard now.
Reading Keys in Editor
Next, add OnChar() with ClassWizard.
void CEditorView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CView::OnChar(nChar, nRepCnt, nFlags);
}
We store the text in a member object of the document, which we call m_data. We place all our data in m_objectnot just the string of text, but also its location in the view (which the user can specify with the mouse), so that when the user reads this object back in from a data file, the text can be restored at its correct location.
To keep our data private to the m_data object, we use access functions when working with m_object. This is standard practice in real-world programming. In particular, we use SetText() and GetText() to work with the text in m_data, so we can add the newly struck character to m_data.
void CEditorView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CEditorDoc* pDoc = GetDocument(); ⇐
ASSERT_VALID(pDoc); ⇐
pDoc->m_data.SetText(pDoc->m_data.GetText() += nChar); ⇐
CView::OnChar(nChar, nRepCnt, nFlags);
}
Then we invalidate the view, set the documents modified flag, and update all views.
void CEditorView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CEditorDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_data.SetText(pDoc->m_data.GetText() += nChar);
Invalidate(); ⇐
pDoc->SetModifiedFlag(true); ⇐
pDoc->UpdateAllViews(this); ⇐
CView::OnChar(nChar, nRepCnt, nFlags);
}
Weve seen all this before, so now we add a new compatibility: letting the user delete characters.
Deleting Characters
Deleting characters is easy: We only have to remove the last character in the text string. We do that with the CString Left() function in case the user has typed the backspace character (which is represented by \b).
void CEditorView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CEditorDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(nChar == \b) ⇐
pDoc->m_data.SetText(pDoc->m_data.GetText().Left
(pDoc->m_data.GetText().GetLength() - 1)); ⇐
else ⇐
pDoc->m_data.SetText(pDoc->m_data.GetText() += nChar);
Invalidate();
pDoc->SetModifiedFlag(true);
pDoc->UpdateAllViews(this);
CView::OnChar(nChar, nRepCnt, nFlags);
}
Deleting a Character
Another way of deleting a character on the screen aside from redrawing the entire string is to get the background color of the drawing device context with GetBkColor(), make that the text-drawing color with SetTextColor(), then redraw the character in the background color.
Now that weve enabled character reading, we should set up the m_data object that will store our programs data.
Designing the Editor Programs Data Storage
We store the Editor programs data in the object named m_object; this is an object of a new class we need to design named CData.
class CData{
};
This class stores the actual text in the document m_text, as well as the texts position in the view, m_x and m_y, as private data members.
class CData{
private:
CString m_text;
int m_x, m_y;
};
We add a default constructor, initializing our data.
class CData{
private:
CString m_text;
int m_x, m_y;
public:
CData() {m_text = CString(); m_x = m_y = 0;} ⇐
};
|