Link to home
Start Free TrialLog in
Avatar of mwcmp
mwcmp

asked on

Can I change the colour of a CDialog and CFormView?

Can I change the (bkgd) colour of a CDialog and CFormView? If so, how?
SOLUTION
Avatar of migel
migel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mwcmp
mwcmp

ASKER

error C2065: 'CTLCOLOR_DLG' : undeclared identifier
warning C4018: '==' : signed/unsigned mismatch
Avatar of mwcmp

ASKER

BTW. I had created m_BackBrush as gobal as I do not get what you mean by creating it in the OnInitDialog.
What I mean is, I created it as:
CBrush m_BackBrush(RGB(255,0,0));

How then can I create it in OnInitDialog, but use it in OnCtlColor?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Then it will be available in OnCtlColor
 if (nCtlColor == (UINT) CTLCOLOR_DLG || (UINT) CTLCOLOR_STATIC  == nCtlColor)
   return  (HBRUSH)m_BackBrush;
Avatar of mwcmp

ASKER

Still the same error:
>error C2065: 'CTLCOLOR_DLG' : undeclared identifier

Do I need to include anything? BTW, I am using EVC.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mwcmp

ASKER

>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?
Sorry, translating from my own code...
'fondo' must 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* pDC)
{
   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( RGB( 0, 255 - (i * 4), 255 - (i * 4) ));
      
   CWnd* pWnd = pDC->GetWindow();
   pWnd->GetClientRect(&rectWnd);
   
   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      
Avatar of mwcmp

ASKER

Hi Jim

>Brush pBrush[64];
This should be CBrush instead? As I get an error from it.

and also this error>>
>>error C2039: 'GetWindow' : is not a member of 'CDC'
        C:\Windows CE Tools\wce300\Pocket PC 2002\mfc\include\afxwin.h(590) : see declaration of 'CDC'
CWnd* pWnd = pDC->GetWindow();
Avatar of mwcmp

ASKER

Hi Jaime

error C2065: 'CTLCOLOR_SCROLLBAR' : undeclared identifier
error C2051: case expression not constant
 error C2065: 'CTLCOLOR_DLG' : undeclared identifier
error C2051: case expression not constant

Wat's the problem? Why  I keep having the same error...
Yes, Brush should be CBrush.
When I look up the help for CDC, GetWindow() is a member function - at least in non-Windows CE land.  Is this your platform?
Jim
Avatar of mwcmp

ASKER

Win CE
ok, well, then get the window from the view itself.
the this pointer is the pointer to the CWnd* of the view

CWnd* pWnd = (CWnd *) this;

Jim
Avatar of mwcmp

ASKER

my formView remains white..
Avatar of mwcmp

ASKER

Jim

Do you have any idea what went wrong?
Try just calling GetClientRect() - you're already in the view class.  Remove the call to get the CWnd * - it's not necessary.
Can you step thru the code?  I don't have a WinCE setup so I don't know what it entails for debugging.
Jim
Avatar of mwcmp

ASKER

Ok. There is no error, but the view is still white.
Any more suggestion?
Comment out the call to CView::OnEraseBkgnd(pDC) - although I don't know what affect that will have.
Jim
Avatar of mwcmp

ASKER

Haha. Still the same results.
I think OnCtlColor is the proper way.
Avatar of mwcmp

ASKER

But the problem is I have all these errors

error C2065: 'CTLCOLOR_SCROLLBAR' : undeclared identifier
error C2051: case expression not constant
 error C2065: 'CTLCOLOR_DLG' : undeclared identifier
error C2051: case expression not constant
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mwcmp

ASKER

Jaime
Ok. Thanks. It works now.
Can that piece of code work for CFormView too?
Not tried, just do it.
It must be a WinCE thing - because the code I gave you works on a Windows PC - Win2K.
Jim
Avatar of mwcmp

ASKER

It can't seems to work for view.
If that of a dialog is CTLCOLOR_DLG, what's the value for that o a view?
Avatar of mwcmp

ASKER

What is the equivlent of OnInitDialog for a view?
OnInitialUpdate()
Does WinCE send a WM_ERASEBKGND message?
Avatar of mwcmp

ASKER

>Does WinCE send a WM_ERASEBKGND message?
I suppose not.


OnCtlColor don't seems to work for view
Can you debug your app in WinCE?
OnCtlColor is used for windows controls that are child controls of a window - doesn't get called when a view has no controls.

What if you try setting the the brush color to one color RGB(0,0,255) instead of multiple brushes...
Jim
Avatar of mwcmp

ASKER

Hmm.. Can't seems to work either
Avatar of mwcmp

ASKER

How abotu RedrawWindow or using a CStatic?
I read about people using them to change their view colour. But I tried and it still remains white. How should I use them?
Avatar of mwcmp

ASKER

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_CENTER,
            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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mwcmp

ASKER

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( RGB( 0, 255 - (i * 4), 255 - (i * 4) ));

      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?