Overriding CWnd::WindowProc

The MFC skeleton that AppWizard creates contains several derived classes in which you can place your overrides of base class functions, including the view class, derived from CView.

CView is derived in turn from class CWnd, the base class of all MFC windows. CView inherits the WindowProc member function from CWnd. So does your derived view class— until you override the function, supplying your own version. For your view class, MFC calls the overriding version instead of the default version used for other windows.

If you had to override one of the member functions of class CWinApp earlier in this process, you know how to override a class member function. If not, follow these directions, which illustrate the general case with specific code for the WindowProc override you need for your C++ conversion:

Ž To override a class member function

1. Derive your own class, declaring the appropriate MFC class as its base class.

The syntax looks like this:


class CMyClass : public CBaseClass { // class member variables and member functions };

You can use ClassWizard to derive classes from MFC base classes and to perform parts of the next two steps.

2. In the .H file for your class, add a prototype for the overriding function.

For example, for any application you're converting to C++ with this migration guide, add a prototype for the WindowProc function in the view class (which AppWizard derives for you). Because it's a virtual function, a good place to put it is in an "// Overrides" section (with a "protected" specifier) as shown here:


class CShowDibView : public CView { // ... // Overrides // ... protected: virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); // ... // Implementation // ... DECLARE_MESSAGE_MAP() };

Notice that WindowProc is a virtual function. The virtual keyword is redundant in an override, but it serves as a reminder of this function's nature. The protected keyword means that WindowProc can only be called from within objects of class CShowDibView or classes derived from it. You can't call it from outside the class.

Tip In Visual C++ 2.0, ClassWizard will override virtual functions for you; all you have to do is fill in the function body in the .CPP file. In earlier versions, you must write the function prototype and body yourself. For syntax, see class CWnd in the Class Library Reference, or see your C++ documentation.

3. In the .CPP file for your class, add a function definition and fill in the details of what the overriding version does.

For example, the following code shows a WindowProc override for SHOWDIB's view class, added to the SHOWDVW.CPP file:


LRESULT CShowDibView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { // your message handling code will go here return CView::WindowProc(message, wParam, lParam); }

Notice that the override calls the WindowProc function in the base class, which this version overrides. In other words, CShowDibView's version of WindowProc does what it needs to do; then it calls the base class version to let the base class process any messages it wants to process.

For SHOWDIB, override WindowProc in the view class, as described above. AppWizard has already done step 1 for you; class CShowDibView is the derived view class. Complete steps 2 and 3 using ClassWizard. (For the 16-bit version, you'll have to do this override by hand; if you're using Visual C++ 2.0, ClassWizard can override virtual functions, but not in earlier versions.)

Tip Calling the base class version from an override is common in MFC. Whether you call the base class version before your own code, after it, or in the middle depends on your needs and on what the base class version does. Sometimes you want to override it completely; sometimes, as here, you just want to augment what the base class does. When you use ClassWizard to do the override, it suggests the most usual case by writing the base-class call for you. You can usually change its position if you need to.