error C2065: 'CTLCOLOR_DLG' : undeclared identifier
warning C4018: '==' : signed/unsigned mismatch
Main Topics
Browse All TopicsCan I change the (bkgd) colour of a CDialog and CFormView? If so, how?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
in your dialog class declaration
CBitmap m_bg;
in your dialog constructor:
m_bg.CreateSolidBrush(RGB(
Add OnCtrlColor handler:
HBRUSH CYourDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (!m_bg.GetSafeHandle())
return hbr;
switch (nCtlColor) {
//Edit controls need white background and black text
//Note the 'return hbr' which is needed to draw the Edit
//control's internal background (as opposed to text background)
case CTLCOLOR_EDIT:
pDC->SetTextColor(RGB(0,0,
pDC->SetBkColor(RGB(255,25
return hbr;
//Static controls need black text and same background as m_brush
case CTLCOLOR_STATIC:
LOGBRUSH logbrush;
fondo.GetLogBrush( &logbrush );
pDC->SetTextColor(RGB(0,0,
pDC->SetBkColor(logbrush.l
return m_bg;
// change background for some controls
case CTLCOLOR_LISTBOX:
case CTLCOLOR_SCROLLBAR:
case CTLCOLOR_BTN:
case CTLCOLOR_MSGBOX:
// change background for dialog itself
case CTLCOLOR_DLG:
return m_bg;
default:
return m_bg;
}
}
>CBitmap m_bg;
Should this be CBrush? If so, I had changed it...
few errors>>
error C2065: 'fondo' : undeclared identifier
error C2228: left of '.GetLogBrush' must have class/struct/union type
error C2065: 'CTLCOLOR_SCROLLBAR' : undeclared identifier
error C2051: case expression not constant
error C2065: 'CTLCOLOR_DLG' : undeclared identifier
error C2051: case expression not constant
>fondo.GetLogBrush( &logbrush );
Should fondo be m_bg?
For a CFormview just handle the WM_ERASEBKGND message in the view class - OnEraseBkhgnd(CDC* pDC).
Here you get the DC of the view window. You can then draw anything you want in this window. For example - if you wanted to have a gradient background:
BOOL CMyView::OnEraseBkgnd(CDC*
{
CView::OnEraseBkgnd(pDC);
Brush pBrush[64]; // use 64 shades of cyan
CRect rect,rectWnd;
// Create brushes
for( int i = 0; i < 64; i++ )
pBrush[i].CreateSolidBrush
CWnd* pWnd = pDC->GetWindow();
pWnd->GetClientRect(&rectW
int nWidth = rectWnd.right;
int nHeight = rectWnd.bottom;
// Paint screen
for( i=0; i < nHeight; i++ )
{
SetRect( &rect, 0, i, nWidth, i + 1);
pDC->FillRect( &rect, &pBrush[ ( i * 63 ) / nHeight ] );
}
// release brushes to Windows
for( i = 0; i < 64; i++ )
DeleteObject( pBrush[i] );
return TRUE;
}
Jim
void CGuiderView::OnDraw(CDC* pDC)
{
CGuiderDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CStatic myStatic;
CRect rect;
GetClientRect(&rect);
int nX = rect.left;
int nY = rect.top;
myStatic.Create(NULL, WS_CHILD|WS_VISIBLE|SS_CEN
CRect(0,0,nX,nY), this);
CBrush bb(RGB(255,127,127));
pDC->FillRect( &rect, &bb);
myStatic.Invalidate() ;
}
And I manage to change the background colour of my view. But the problem is, the background of my button and static control did not change.
What should I do?
mwcmp,
The pDC is the DC of your view - the controls are not affected.
To change the color of a static you need to handle the WM_CTLCOLOR message. You return an HBRUSH from the call. But in there you can check the id of the control and change its background. Here is some code:
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
switch (nCtlColor)
{
case CTLCOLOR_STATIC:
switch (pWnd->GetDlgCtrlID())
{
case IDC_WELCOMETEXT:
SetStaticFont( pWnd, 6 );
pDC->SetTextColor(RGB(0, 0, 255));
break;
default:
break;
}
break;
default:
break;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
This should do the trick.
Jim
What if I had used the code that you had posted, but only that it is in the OnDraw()
void CGuiderView::OnDraw(CDC* pDC)
{
//CGuiderDoc* pDoc = GetDocument();
//ASSERT_VALID(pDoc);
CBrush pBrush[64]; // use 64 shades of cyan
for( int i = 0; i < 64; i++ )
pBrush[i].CreateSolidBrush
CRect rect,rectWnd;
int nWidth, nHeight;
GetClientRect(&rectWnd);
nWidth = rectWnd.right;
nHeight = rectWnd.bottom;
for( i=0; i < nHeight; i++ )
{
SetRect( &rect, 0, i, nWidth, i + 1);
pDC->FillRect( &rect, &pBrush[ ( i * 63 ) / nHeight ] );
}
for( i = 0; i < 64; i++ )
DeleteObject( pBrush[i] );
}
for this, I cannot set them to gradient colour too, am I right?
the method that you had suggeted only change the colour of the controls into a single colour. Am I right?
Business Accounts
Answer for Membership
by: migelPosted on 2004-08-10 at 12:06:03ID: 11766453
Hi!
C* pDC, CWnd* pWnd, UINT nCtlColor)
You can handle WM_CTLCOLORDLG in the your dialog class
for example:
// m_BackBrush is the CBrush object member of the your class (NOTE it must be created in the construxtor or OnInitDialog methods)
HBRUSH CColoredDlg::OnCtlColor(CD
{
// Call the base class implementation first! Otherwise, it may
// undo what we're trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor == CTLCOLOR_DLG || CTLCOLOR_STATIC == nCtlColor)
return (HBRUSH)m_BackBrush;
return hbr;
}