HOWTO: Change Window Background Color with Foundation Classes

Last reviewed: May 28, 1997
Article ID: Q103786

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
        - Microsoft Visual C++, 32-bit Edition, version 1.0, 2.0, 2.1, 4.0
    

SUMMARY

To change the background color for a CView, CFrameWnd, or CWnd object, process the WM_ERASEBKGND message. The sample code below demonstrates how to do this.

MORE INFORMATION

Sample Code

   BOOL CSampleView::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;
   }

To change the background color for a CMDIFrameWnd, you must subclass the multiple document interface (MDI) client window (window in the client area of CMDIFrameWnd) and process the WM_ERASEBKGND message. For more information about the MDI client window in an MDI application, see chapter 18 in "Programming Windows 3.1 - Third Edition" by Charles Petzold. For an example that shows how to subclass the MDICLIENT window, please see the article in the Microsoft Knowledge Base:

    ARTICLE-ID: Q129471
    TITLE     : HOWTO: Subclass the MDICLIENT by Using MFC

To change the background color of an MDI client window (client area of a CMDIFrameWnd), perform the following steps using an AppWizard-generated application:

  1. Create a generic CWnd class with ClassWizard.

  2. Add a member variable, using the type of the CWnd class from step 1, to the CMainFrame class.

  3. In the CMainFrame's OnCreate member function, after the call to the base class CMDIFrameWnd::OnCreate(), add a call to SubclassWindow(). For example:

          if (!m_wndNewClient.SubclassWindow(m_hWndMDIClient))
          {
    
            TRACE("Failed to subclass MDI client window\n");
            return -1;      // fail to create
          }
    
       m_hWndMDIClient is the member variable of CMDIFrameWnd that
       contains the handle to the MDI client window. Also, replace
       "m_wndNewClient" with the data member that you created in step 2.
    
    

  4. Whenever a window is subclassed, the GetSuperWndProcAddr() member function for the CWnd needs to be overridden to provide storage for the old window procedure's address. To do that, add the following function to the implementation of the CWnd class created in step 1:

          WNDPROC* CNewClientWnd::GetSuperWndProcAddr()
          {
    
             static WNDPROC NEAR pfnSuper = NULL;
             return &pfnSuper;
          }
    
       NOTE: Replace "CNewClientWnd" above with the name of your class.
    
       For more information on subclassing windows using the Microsoft
       Foundation Classes, please see the following materials:
    
       - "Class Library Reference" for functions CWnd::SubclassWindow(),
         CWnd::GetSuperWndProcAddr(), and CWnd::SubclassDlgItem()
    
       - Query on the following words in the Microsoft Knowledge Base:
    
         subclass and sample and mfc
    
       - See the CTRLTEST MFC sample application that is provided with
         Visual C++ for Windows and Visual C++ 32-bit Edition.
    
       - See the article titled "Subclassing Windows with the Microsoft
         Foundation Class Library" on the Microsoft Developer Network
         (MSDN) CD.
    
    

  5. Override the WM_ERASEBKGND message for the generic CWnd class, using the code listed above.

To change the background color of a CFormView object, either process the WM_ERASEBKGND message and use the code above or process the WM_CTLCOLOR message to change the background color.

For more information on changing the background color of a dialog box by processing the WM_CTLCOLOR message, please query on the following words in the Microsoft Knowledge Base:

   changing and background and color and MFC
 

	
	


Keywords : MfcUI kbfasttip kbhowto
Technology : kbmfc
Version : 1.0 1.5 1.51 1.52 2.0 2.1 4.0
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.