![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Weve 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 programOnChar()using ClassWizard. This function will be called every time the user presses a key on the keyboard, and in OnChar() well add the struck key to our string of stored text. Reading Keys in KeysSDIStart 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 programs view class (which well 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 struckthe code for this key is passed to us in the nChar parameterand add it to the text string weve stored in the programs 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 were ready to add the new character to the string of text weve 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, well 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 weve stored in the text member of the document object, and well do that with the OnDraw() function. Displaying Keys in KeysSDITo display the string of typed keys in our programs 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 programs 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 programin Figure 1.9, the program displays the Hello from Visual C++ messagesimply by typing on the keyboard. Our KeysSDI program is a success. Now that weve gotten KeysSDI working, its time to dissect it and see what makes this program tick. Dissecting a Visual C++ ProgramWell 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.
----------------------- ----------------------- | 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 thats loaded first, and it interacts with Windows to get things started. Next, this object loads in the main window and displays it. The programs data is stored in the document object, and displaying that data is the view objects 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. Well 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. Heres 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 Lets take a look at the four major parts of the KeysSDI program now, starting with the application object, KeysSDI.
|
![]() |
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. |