|
-
October 16th, 1999, 03:35 PM
#1
Setting Background color of a view
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
-
October 16th, 1999, 05:03 PM
#2
Re: Setting Background color of a view
Likewise, changing structure cs to change the size of the window too does not work.
-
October 16th, 1999, 07:30 PM
#3
Re: Setting Background color of a view
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
#4
Re: Setting Background color of a view
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) );
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|