Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing Background Color of Parent & (MDI) Child Windows

252 views
Skip to first unread message

VirGin

unread,
Oct 8, 1997, 3:00:00 AM10/8/97
to

HiHiHi,

I am learning to write Win95/98/NT (win32) apps and I would like some help.

I have created an app with the AppWizard. Now I would like to change the background color
of the main window and the child windows. This is very difficult when the AppWizard writes
the code.

I have tried using

GetDC() -> SetBkGndColor ( RGB ( 050, 100, 150 ) );

in every constructor I could think of. I have also tried it in the function which creates
and draws the windows. No Luck.

Can someone help me with changing the window colors when the AppWizard has generated the
code?

thanx


V.


John Bundgaard

unread,
Oct 10, 1997, 3:00:00 AM10/10/97
to

If you want to change the background color of the main window (CMainWnd),
the you will have to subclass the 'CMainWnd::m_hWndMDIClient' window, and
handle the WM_ERASEBKGND message in your own windows procedure.

1. Create a new class derived from CWnd. (CMyWnd)
2. Add a WM_ERASEBKGND message handler to CMyWnd using the class-wizard

//
// This paints the background red !
//
BOOL CMyWnd::OnEraseBkgnd(CDC* pDC)
{
COLORREF color = RGB(255, 0, 0);
CRect rc;
pDC->GetClipBox(&rc);
CBrush brush(color);
pDC->FillRect(&rc, &brush);
brush.DeleteObject();
return TRUE;
}

3. Add CMyWnd as a member variable to CMainFrame - in the header ;-)
CMyWnd m_wndMine;

4. Subclass the m_hWndMDIClient window in CMainFrame::OnCreate()

CMainFrame::OnCreate(..)
{
....
ASSERT(IsWindow(m_hWndMDIClient));
m_wndMine.SubclassWindow(m_hWndMDIClient);
.....
}

5. Compile and run..........

I all other windows that need another background color, just override the
OnEraseBkgnd
function.


VirGin wrote in message <343afcc4...@news.astat.de>...

William G. Hood

unread,
Oct 10, 1997, 3:00:00 AM10/10/97
to

John Bundgaard <piano*bo...@post1.com> wrote in article
<61l8pj$32eg$1...@news.inet.tele.dk>...


> If you want to change the background color of the main window (CMainWnd),
> the you will have to subclass the 'CMainWnd::m_hWndMDIClient' window, and
> handle the WM_ERASEBKGND message in your own windows procedure.
>

You can also change the background color by changing the window's class.
To change the
background to black for example:

SetClassLong( hwnd, GCL_HBRBACKGROUND, (long) GetStockObject(BLACK_BRUSH)
);

Bill Hood
ho...@eivax.ualr.edu


Michael Rice

unread,
Oct 10, 1997, 3:00:00 AM10/10/97
to

The easiest way to do this is to handle WM_ERASEBKGND messages. This will
allow you to draw solid colors, patterns, brushes, bitmaps, etc.

To put a handler in, just use the Class Wizard and add a function to handle
the message in each of the window classes you want to change the background
for. For example, to put a disgusting green background on a window:

BOOL CMyView::OnEraseBkgnd(CDC* pDC)
{
CRect rectClient;
CBrush brushFg;
CBrush* pBrushOld;

GetClientRect(rectClient);

brushFg.CreateSolidBrush(RGB(150, 250, 150));
pBrushOld = pDC->SelectObject(&brushFg);

pDC->Rectangle(rectClient);

pDC->SelectObject(pBrushOld);
brushFg.DeleteObject();

return TRUE;
}


VirGin wrote in message <343afcc4...@news.astat.de>...
>HiHiHi,
>
>I am learning to write Win95/98/NT (win32) apps and I would like some help.
>

>I have created an app with the AppWizard. Now I would like to change the
background color

0 new messages