Click here to Skip to main content
13,346,843 members (24,103 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 12 Apr 2007

Change the background color of the frame area of an MDI application.

, 12 Apr 2007
Rate this:
Please Sign up or sign in to vote.
This article describes how to change the color of Frame Windows client area by subclassing the control which draws the background.

Screenshot - MDIFrameBackground.jpg


This is actually a very simple task. The hard thing to figure out is the fact that the background part of the main frame Window isn't really handled by the frame Window itself. It contains another Window that fills the area inside the frames border. This Window is known as m_hWndMDIClient.


To get this task done, my first instinct was to catch the WM_ERASEBKGND in CMainFrame and do the FillSolidRect there. But I soon realized that approach didn't do anything other than make the frame flicker and show red only when it's being resized, otherwise the gray background was completely intact. Not exactly what I was looking for. Suddenly it became a little mystery. Why doesn't the background of the frame change when I fill it with a color in the frame's WM_ERASEBKGND handler? So I went on a hunt, I went through a lot of the CMDIFrameWnd and CFrameWnd code looking for methods that would draw on the frame, and there was not much there. I finally ended up at CMainFrame::OnCreate method, I set a breakpoint and stepped through the CMDIFrameWnd::OnCreate method. After what seemed to be hours of searching I came across the following code in CMDIFrameWnd::CreateClient:

// Create MDICLIENT control with special IDC
if ((m_hWndMDIClient = ::CreateWindowEx(dwExStyle, _T("mdiclient"), NULL,
          dwStyle, 0, 0, 0, 0, m_hWnd, (HMENU)AFX_IDW_PANE_FIRST,
          AfxGetInstanceHandle(), (LPVOID)&ccs)) == NULL)
   TRACE(traceAppMsg, 0, _T("Warning: CMDIFrameWnd::OnCreateClient: 
                                             failed to create MDICLIENT.")
            _T(" GetLastError returns 0x%8.8X\n"), ::GetLastError());
   return FALSE;

It is pretty clear that the frame Window is creating another Window to act as its center (To this day I don't really know the purpose behind this). So that's where this code comes in. By subclassing the client Window m_hWndMDIClient I was able to cleanly change the color of the MDI frame Window.

Using the code

To change the color of the background, first we must create a new CWnd derived class which handles the WM_ERASEBKGND message and fills the client area of the Window with a different color. So let's add a class called CClientWnd which inherits from CWnd:

#pragma once

// CClientWnd
class CClientWnd : public CWnd

   virtual ~CClientWnd();


   afx_msg BOOL OnEraseBkgnd(CDC* pDC);

And implement the OnEraseBkgnd method:

// ClientWnd.cpp : implementation file

#include "stdafx.h"
#include "ClientWnd.h"

// CClientWnd





// CClientWnd message handlers
BOOL CClientWnd::OnEraseBkgnd(CDC* pDC)
   CRect Rect;

   //fill the entire client area with Red!

   //return TRUE to indicate that we took care of the erasing
   return TRUE;

Now the only thing left to do is to subclass the Window m_hWndMDIClient. To do that we are going to overwrite CMainFrame::OnCreateClient method, and declare m_Client in CMainFrame's class declaration as a private variable.

class CMainFrame : public CMDIFrameWnd
   virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);

   CClientWnd   m_Client;

Implementation of OnCreateClient should look like this:

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
   if (CMDIFrameWnd::OnCreateClient(lpcs, pContext))
      return TRUE;

   return FALSE;

Have fun!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ali Rafiee
United States United States
Ali Rafiee has been developing windows applications using C++ since 1991, and he hasn't looked back since. Ali has been a software development consultant for must of his career, but he has finally settled down and has been working for an educational software company since 2000. While he is not working, he is either learning C#, flying airplanes, playing with his daughter, or answering peoples question on newsgroups, he finds that to be a great learning tool for himself.

Ali is also a Microsoft Visual C++ MVP.

You may also be interested in...


Comments and Discussions

QuestionThank you + try gradients Pin
Member 968830920-Jun-13 15:51
memberMember 968830920-Jun-13 15:51 
GeneralThank you! Pin
Nguyen Van Thai23-Jul-11 5:32
memberNguyen Van Thai23-Jul-11 5:32 
GeneralThanks Pin
bilelbenfredj16-May-08 0:18
memberbilelbenfredj16-May-08 0:18 
GeneralI like your approach Pin
John Simmons / outlaw programmer28-Apr-07 2:18
mvpJohn Simmons / outlaw programmer28-Apr-07 2:18 
GeneralAnother way Pin
Piotr Szewczyk17-Apr-07 5:57
memberPiotr Szewczyk17-Apr-07 5:57 
GeneralRe: Another way Pin
Ali Rafiee18-Apr-07 15:40
memberAli Rafiee18-Apr-07 15:40 
GeneralThanks Pin
James Armstrong16-Apr-07 23:52
memberJames Armstrong16-Apr-07 23:52 
GeneralThank You Pin
shawkins16-Apr-07 9:16
membershawkins16-Apr-07 9:16 
GeneralRe: I got a rating of 2 Pin
Hans Dietrich12-Apr-07 21:06
mvpHans Dietrich12-Apr-07 21:06 
GeneralRe: I got a rating of 2 Pin
Ali Rafiee13-Apr-07 6:00
memberAli Rafiee13-Apr-07 6:00 
GeneralRe: I got a rating of 2 Pin
kenearle14-Apr-07 8:55
memberkenearle14-Apr-07 8:55 
Same problem for me[^], with replies from Hans Dietrich and Chris Maunder, not that reading it will likely make you feel any betterSmile | :)
GeneralRe: I got a rating of 2 Pin
code_discuss19-Apr-07 5:34
membercode_discuss19-Apr-07 5:34 
GeneralRe: I got a rating of 2 Pin
Ali Rafiee19-Apr-07 5:53
memberAli Rafiee19-Apr-07 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 12 Apr 2007
Article Copyright 2007 by Ali Rafiee
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid