Click to See Complete Forum and Search --> : Setting Background color of a view


Jeff Sargent
October 16th, 1999, 03:35 PM
I want to change the background color of my CEditView from white to gray before it is shown. In PreCreateWindow(CREATESTRUCT& cs) I have a call to SetClassLong. Well, here's the code:


MyCEditViewClass::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

BOOL bPreCreated = CEditView::PreCreateWindow(cs);
cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
SetClassLong(AfxGetMainWnd()->GetSafeHwnd(), GCL_HBRBACKGROUND, COLOR_BTNFACE);

return bPreCreated;
}




It compiles and runs, but the color of the CEditView remains white. Perhaps I'm getting the HWND the wrong way? If so, how do I do it? Perhaps I need to move the code? OnDraw? Please help :)

Thanks,

Jeff

Alok Govil
October 16th, 1999, 05:03 PM
Likewise, changing structure cs to change the size of the window too does not work.

Jeff Sargent
October 16th, 1999, 07:30 PM
After looking a bit further, it winds up that AfxGetMainWnd() is returning null in the following code:


PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping

SetClassLong(AfxGetMainWnd()->GetSafeHwnd(), GCL_HBRBACKGROUND, COLOR_BTNFACE);

return CEditView::PreCreateWindow(cs);
}




Since AfxGetMainWnd() returns NULL, GetSafeHwnd doesn't work, and I Cant use that HWND value in SetClassLong(HWND, int, long). Any ideas as to why I get NULL, or how to get the HWND a different way? Thanks,

Jeff

October 20th, 1999, 09:07 PM
Well, I don't know if this is what you guys are looking
for (I'm not real knowledgeable about the distinction
between forground and background), but this does set
the color of the CView.

void CChangeColorView::OnDraw(CDC* pDC)
{
CChangeColorDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect lRect;
GetClientRect(lRect);
lRect.NormalizeRect();

// The next three lines would work also, but
// according to the documentation, FillSolidRect is faster.

// CBrush lSolidBrush;
// lSolidBrush.CreateSolidBrush(RGB(0,50,0));
// dc.FillRect(lRect, &lSolidBrush);

pDC->FillSolidRect( lRect, RGB(0,75,0) );
}