Open GL Super Bible

Previous Table of Contents Next


So use the Class Wizard once again to add the two palette message handlers to the CMainFrame class. These handlers simply find the active view and post the palette messages to it unchanged, allowing the view to respond as needed. These message handlers are listed in Listing 21-4.

Listing 21-4 CMainFrame code to route palette-handling messages to the view

// Route message to CView-derived class
void CMainFrame::OnPaletteChanged(CWnd* pFocusWnd)
        {
     CView* pView = GetActiveView();
     if (pView)
                 {
                 // OnPaletteChanged is not public, so send a message.
	            pView->SendMessage(WM_PALETTECHANGED,
                               (WPARAM)(pFocusWnd->GetSafeHwnd()),
                               (LPARAM)0);
                 }
         }

// Route message to CView-derived class.
BOOL CMainFrame::OnQueryNewPalette()
        {
        CView* pView = GetActiveView();
        if (pView)
                {
                // OnQueryNewPalette is not public, so send a message.
                return pView->SendMessage(WM_QUERYNEWPALETTE,
                                  (WPARAM)0,
                                  (LPARAM)0);
                }

        return FALSE;
        }

We also added a WM_TIMER handler and created a timer in our view class to create an animated OpenGL scene (Figure 21-4). The timer function simply invalidates the window, forcing a repaint. In our glcode.c module the rendering function increments a rotation angle each time the screen is redrawn, thus creating the animation effect. All of this code is in the \MFCGL directory on the CD.


Figure 21-4  Final animated output of our MFC sample

Summary

This chapter covered the specific mechanics of using OpenGL from an MFC-based program, demonstrating where to set the Windows styles required for OpenGL, where and when to set the pixel format, and creation of the rendering context. The example program also illustrates when and where to make the rendering context current, and how to realize an MFC CPalette when needed.

You should be able to take the sample application from this chapter and easily add your own custom OpenGL code. In addition, the frameworkŚwith all the OpenGL code in the glcode.c moduleŚmakes it easy to port existing C/OpenGL samples to our MFC shell program. You can study additional examples in many of the sample programs in this book, which are implemented in C, and in C++ using MFC and OWL (see Chapter 22).


Previous Table of Contents Next