tags:

views:

434

answers:

4

Is there a way to change the color of the background for a MDIParent windows in MFC (2005)?

I have tried intercepting ON_WM_CTLCOLOR AND ON_WM_ERASEBKGND but neither work. OnEraseBkgnd does work, but then it gets overwritten by the standard WM_CTL color.

Cheers

A: 

Just guessing here, but try handling ON_WM_PAINT.

Mark Ransom
+2  A: 

The CMDIFrameWnd is actually covered up by another window called the MDIClient window. Here is a Microsoft article on how to subclass this MDIClient window and change the background colour. I just tried it myself and it works great.

http://support.microsoft.com/kb/129471

Adam Pierce
+1  A: 

Create a class deriving CWnd (CClientWnd for example)

In your CWnd-derived class handle

afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint(void);
afx_msg void OnSize(UINT nType, int cx, int cy);

You need the following message map entries:

ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_SIZE()

In OnEraseBkgnd just return TRUE, you'll do all of the work in OnPaint

In OnPaint, do whatever you like. To fill with a color, you could do

CBrush brush;
brush.CreateSolidBrush(COLORREF(RGB( 80, 160, 240 )));

CRect clientRect;
GetClientRect(clientRect);

CPaintDC dc(this);
dc.FillRect(clientRect, &brush);

In OnSize call the superclass, then invalidate to force a repaint.


In your mainframe, declare a member CClientWnd (m_clientWnd for example)

In your mainframe's OnCreate, first call the superclass, then

m_clientWnd.SubclassWindow(m_hWndMDIClient);
Aidan Ryan
A: 

This works (( http://support.microsoft.com/kb/129471 )) OK when I use CMDIFrameWnd

But if I create the same Application using CMDIFrameWndEx instead of CMDIFrameWnd it no longer works!!! I get an Assertion Error on the SubClassWindow() call. Any Clues, thought, or ideas why this is happening?

Thank You