Microsoft KB Archive/129471

From BetaArchive Wiki
Knowledge Base


How to create the MDIClient window by using MFC

Article ID: 129471

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.51
    • Microsoft Visual C++ 1.52 Professional Edition
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 2.0 Professional Edition
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 4.1 Subscription
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++ 6.0 Standard Edition
    • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition



This article was previously published under Q129471

Note Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework, and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

SUMMARY

In MFC, the MDICLIENT window is stored in a public HWND member variable (m_hwndMDIClient) in the CMDIFrameWnd class. CMDIFrameWnd is the base class of the CMainFrame class in an AppWizard-generated MDI application.

To subclass the MDICLIENT window, you must perform the following three steps :

  1. Use ClassWizard to derive a class from CWnd that is named CMDIClientWnd.
  2. Add theGetSuperWndProcAddr() function to CMDIClientWnd.
  3. Use CMDIClientWnd to subclass the MDICLIENT window.

After you have subclassed the MDICLIENT window with CMDIClientWnd, message handlers and other functions can be placed in the CMDIClientWnd class.

MORE INFORMATION

Here is more detailed information about each of the steps:

  1. Use ClassWizard to derive a class from CWnd that is named CMDIClientWnd.

    For details on how to derive a class using ClassWizard, see the User's Guide documentation on ClassWizard, specifically the "Adding a New Class" section.

    Note Visual Studio .NET does not have ClassWizard. Instead, use the Add Class dialog box to access the MFC Class Wizard. For more information see the MSDN topic "Where Are ClassWizard and WizardBar in Visual C++ .NET?"
  2. Add the GetSuperWndProcAddr() function to CMDIClientWnd.

    Note You only have to perform this step if you are using 16-bit versions of Visual C++, not 32-bit. The 32-bit versions of Visual C++ implement this functionality for you.

    Once the class has been created, add the following prototype to the header file:

          public:
              WNDPROC* GetSuperWndProcAddr();
                            

    And add the following function to the .CPP file:

          WNDPROC* CMDIClientWnd::GetSuperWndProcAddr() {
    
              static WNDPROC NEAR pfnSuper = NULL;
              return &pfnSuper;
          }
                            
  3. Use CMDIClientWnd to subclass the MDICLIENT window in the CMDIFrameWnd class (typically CMainFrame).

    To the CMainFrame class, add a public variable of type CMDIClientWnd that is named m_wndMDIClient. Then modify OnCreate for CMainFrame as follows:

          int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
          {
              if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
              return -1;
    
              if (!m_wndMDIClient.SubclassWindow (m_hWndMDIClient)) { // Add
                  TRACE ("Failed to subclass MDI client window\n");   // Add
                  return (-1);                                        // Add
              }                                                       // Add
          ...
          }
                            

After completing these three steps, you can use ClassWizard to add message handlers to CMDIClientWnd similar to the one below. This changes the background color of MDICLIENT.
Note In Visual Studio .NET, you can add a message handler by selecting CMDIClientWnd in ClassView and in Properties under Messages choose WM_ERASEBKGND message handler method (“OnEraseBkgnd”).

BOOL CMDIClientWnd::OnEraseBkgnd(CDC* pDC)
{
  // Set brush to desired background color
  CBrush backBrush(RGB(255, 128, 128));

  // Save old brush
  CBrush* pOldBrush = pDC->SelectObject(&backBrush);

  CRect rect;
  pDC->GetClipBox(&rect);     // Erase the area needed
  pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),
      PATCOPY);
  pDC->SelectObject(pOldBrush);
  return TRUE;
}
                

Keywords: kbhowto kbprogramming kbmdi KB129471