home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next



Initializing CStrings

It’s not technically necessary to initialize a CString object to an empty string, because it’s automatically initialized that way unless you specify otherwise. You can also use the CString Empty() member function to set a CString object to the empty string.



Figure 1.8  Creating the OnChar() function with ClassWizard.

We’ve set up our data storage at this point. The next step is to get some data to store. We do that by connecting a new member function to our program—OnChar()—using ClassWizard. This function will be called every time the user presses a key on the keyboard, and in OnChar() we’ll add the struck key to our string of stored text.

Reading Keys in KeysSDI

Start ClassWizard now using the ClassWizard item in the View menu, as shown in Figure 1.8. We want to add the new function, OnChar(), to the CKeysSDIView class, so make sure that class appears in the ClassWizard Class name list box. This class is the program’s view class (which we’ll review later in this chapter).

Now find and double-click the WM_CHAR entry in the Messages box, creating a new function named OnChar() attached to the WM_CHAR Windows message. When the user types a key, Windows generates WM_CHAR message, and that, in turn, means our OnChar() function will be called.

To open the new OnChar() function, double-click the OnChar() entry in the ClassWizard Member Functions box.

void CKeysSDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: Add your message handler code here and/or call default

    CView::OnChar(nChar, nRepCnt, nFlags);
}

Visual C++ has provided us with a prompt indicating that we should provide our own message-handling code here, and we will. In this case, we want to take the key the user has struck—the code for this key is passed to us in the nChar parameter—and add it to the text string we’ve stored in the program’s document object.

We begin by getting a pointer, pDoc, to the document object.

void CKeysSDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CKeysSDIDoc* pDoc = GetDocument();                              ⇐
    ASSERT_VALID(pDoc);                                             ⇐

    CView::OnChar(nChar, nRepCnt, nFlags);
}

Now we’re ready to add the new character to the string of text we’ve named text in the document.

void CKeysSDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CKeysSDIDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDoc->text += nChar;                                          ⇐

    CView::OnChar(nChar, nRepCnt, nFlags);
}

Now that we have stored the new key, we need to display it. As with most standard Visual C++ programs, we’ll use the OnDraw() function for that. This function takes care of displaying the data we want displayed. To make sure OnDraw() is called, we invalidate the program display with a call to Invalidate().

void CKeysSDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CKeysSDIDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDoc->text += nChar;
    CView::OnChar(nChar, nRepCnt, nFlags);

    Invalidate();                                                   ⇐
}

All that remains, then, is to display the keys we’ve stored in the text member of the document object, and we’ll do that with the OnDraw() function.

Displaying Keys in KeysSDI

To display the string of typed keys in our program’s window, we will use the TextOut() function in OnDraw(). Visual C++ has already given our program an OnDraw() function in KeysSDIView.cpp.

void CKeysSDIView::OnDraw(CDC* pDC)
{
    CKeysSDIDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    // TODO: add draw code for native data here
}

All we have to do is replace the TODO prompt line with a line like this to display the text, starting at (0, 0) (that is, the upper-left corner) of the program’s window:

void CKeysSDIView::OnDraw(CDC* pDC)
{
    CKeysSDIDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDC->TextOut(0, 0, pDoc->text);                               ⇐
}

That completes KeysSDI. Run the program now, as shown in Figure 1.9. You can type messages into the program—in Figure 1.9, the program displays the “Hello from Visual C++” message—simply by typing on the keyboard. Our KeysSDI program is a success.

Now that we’ve gotten KeysSDI working, it’s time to dissect it and see what makes this program tick.

Dissecting a Visual C++ Program

We’ll continue our Visual C++ review by dissecting KeysSDI. There are four main parts to this and all standard Visual C++ programs: the application object, the main window object, the document object, and the view object.


Figure 1.9  Reading keys with the KeysSDI program.

 -----------------------         -----------------------
| Application Object    |       | Main Window Object   |
|                       |       |                      |
| Handles the interface |       | Handles the main     |
| to Windows            |-------| window itself        |
|                       |       |                      |
 -----------------------         -----------------------
        |                              |
 -----------------------         -----------------------
| Document Object       |       | View Object          |
|                       |       |                      |
| Handles storing of    |-------| Handles displaying   |
| the program data      |       | the program data     |
|                       |       |                      |
 -----------------------         -----------------------

What do these parts do? The application object is the object that’s loaded first, and it interacts with Windows to get things started. Next, this object loads in the main window and displays it. The program’s data is stored in the document object, and displaying that data is the view object’s job.

A different program file exists for each of the main objects in this program. These files are KeysSDI.cpp, MainFrm.cpp, KeysSDIDoc.cpp, and KeysSDIView .cpp. We’ll see all of these files in the remainder of this chapter. Each of these files supports a class corresponding to the application, main window, document, or view. Here’s an overview of how those classes interact and what functions you can use to reach one object from another.

                                KeysSDI.exe
 --------------------------------------------------------------------
| Application               Mainframe Window              View             |
|  --------------------   ---------------------       ---------------      |
||    CKeysSDIApp      | |     CMainFrame      |     | CKeysSDIView  |     |
||                     | |                     |     |               |     |
||                     | |     GetActiveView   |---->|               |     |
||                     | |                     |     |               |     |
||                     | |                     |---->|OnActivateView |     |
||                     | |                     |---->|OnDraw         |     |
||Run() contains the   | |                     |<----|GetParent      |     |
||message loop:        | |                     |---->|OnUpdate<----------
||                     | |                     |  -->|               |     |
|
||        -----        | |                     | |   |GetDocument    |     |
|
Windows  |      |      | |                     | |    -----|---------      |
|
-------->| Loop |------->|                     | |        V Document       |
|
Message  |      |      | |                     | |    ---------------      |
|
||        -----        | |                     | |   | CKeysSDIDoc   |     |
|
||          |          | |                     | |   |               |     |
|
||          |          | | GetActiveDocument   |-|-->|               |     |
|
||          |          | |                     | |   |               |     |
|
||          |          | |                     | |   |UpDateAllViews-----
||          |          | |                     |  ---|GetFirstViewPos|     |
||          |          | |                     |---->|OnNewDocument  |     |
||          |          | |                     |---->|OnOpenDocument |     |
||          |          | |                     |---->|OnSaveDocument |     |
| ----------|----------   ---------------------       ---------------      |
 -----------|---------------------------------------------------------
            V
          Exit

Let’s take a look at the four major parts of the KeysSDI program now, starting with the application object, KeysSDI.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.