Click Here!
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.

Cutting Edge Direct 3D Programming
(Publisher: The Coriolis Group)
Author(s): Stan Trujillo
ISBN: 1576100502
Publication Date: 11/01/96

Bookmark It

Search this book:
 
Previous Table of Contents Next


Creating an MFC Application

Creating applications with AppWizard is so easy that we should create one right now just for practice. From Developer Studio, pull down the File menu and select New. The New window, shown in Figure 1.3, will appear. Choose Project Workspace and click on the Create button.


Figure 1.3  The New window.

Next, the New Project Workspace window appears (Figure 1.4). This window allows you to use any of the AppWizards that are installed on your computer. We’ll use the MFC AppWizard to create an MFC-based Multi-Document application. Select MFC AppWizard(exe). Then enter the name of the project. As Figure 1.4 shows, we will use “Sample” as the name in this example.


Figure 1.4  The New Project Workspace window.

Notice that the project name is also used as the directory where the new project will be created. You can modify the path in the Location box to specify another location.

After you click on the Create button, the first dialog in the MFC AppWizard will appear. This dialog is shown in Figure 1.5. Here, you can specify if you want to create a Single Document Interface (SDI) application, a Multiple Document Interface (MDI) application, or a dialog-based application. In this example, we’ll use the default selection (MDI).


Figure 1.5  The first MFC AppWizard dialog.

Pressing Next takes you to the next dialog. Dialogs 2 through 5 let you specify more options for your new application, and we will use the default settings. Figure 1.6 shows the sixth and final MFC AppWizard dialog.


Figure 1.6  The final MFC AppWizard dialog.

The final dialog allows you to override the names of the classes that will be used in the new project. AppWizard creates suggested class names by appending identifiers such as App, View, and Doc to the project name. For our purposes, the proposed names are acceptable. Press the Finish button to proceed.

Before actually creating the application, AppWizard displays a New Project Information window (Figure 1.7) that outlines the settings to be used.


Figure 1.7  New Project Information window.

This is your last chance to review your choices. Pressing the OK button creates the application. You can compile the new project by pressing F7, and run the application by pressing F5.

Adding an Event Handler

Now that we have a working application, we can modify it with ClassWizard. Using the MFC application that we just created, select ClassWizard from the View menu. Figure 1.8 shows the ClassWizard dialog.


Figure 1.8  The ClassWizard dialog.

The ClassWizard dialog has five tabs. The Message Maps tab should be selected, as shown in the figure. The dialog shows any installed event handlers for the class shown in the Class name pull-down control. The list box labeled Object IDs contains the class name and a list of menu IDs. You add event handlers by selecting one of the menu IDs or the class name and selecting a message from the list box labeled Messages. Pressing the Add Function button creates the new event handler.

Let’s create an event handler that will report keystrokes. This can be done by installing an event handler for the WM_KEYDOWN message. Windows posts a WM_KEYDOWN message when any key is pressed.

Make sure that the CSampleView class is selected in the Class name box and in the Object IDs list box. Select the WM_KEYDOWN entry in the Messages list box (you’ll probably have to scroll through the messages to find WM_KEYDOWN). Once you have it selected, press the Add Function button. AppWizard adds WM_KEYDOWN to the Member functions list box and adds the new event handler to your code. Now press the Edit Code button. ClassWizard will display a window with the new code. Figure 1.9 shows the new event handler.


Figure 1.9  Our new WM_KEYDOWN event handler.


Tip:  Pay attention to event handler code
Pay close attention to the code that ClassWizard places in new event handlers. ClassWizard will indicate the best place to put your code by inserting a comment at the location. Sometimes, this code goes before the call to the base class member function, and sometimes it should go afterward. In some cases, ClassWizard will instruct you not to call the base class function at all.

The only thing that the event handler that ClassWizard creates for you does is call the base class version of the same member function. We can add our code where ClassWizard has left a comment to indicate the best place for our code. For this example, we’ll use a TRACE debug macro to display information about the messages that are received. Modify the event handler to look like this:

void CSampleView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    TRACE("WM_KEYDOWN message received  ");
    TRACE("keycode=%d  repeat=%d  flags=%d\n", nChar, nRepCnt, nFlags);

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

Now, compile and run the application. Once the application window appears, press some keys. The Debug window in Developer Studio will display the TRACE messages.


Note:  This example only works when the project is compiled in Debug mode and executed from Developer Studio because TRACE macros have no effect in Release mode.


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.