Solved

Change color of controls.

Posted on 2000-03-06
5
1,042 Views
Last Modified: 2013-11-20
I have a splitter window with 2 formviews. On the left pane I have changed the color of the entire form using the following code in OnPaint:

CRect rect;
GetClientRect( rect );
dc.FillSolidRect( rect, RGB( 128, 128, 128 ) );

That code works just fine. But now I want to place a button on that form, and I want the button to have the same color as the background. How can I do that? I have tried to use the following code in the OnCtlColor handler:

CBrush brush;
LOGBRUSH logbrush;
brush.CreateSolidBrush (RGB( 128, 128, 128 ));
if ( nCtlColor == CTLCOLOR_BTN )
{
  brush.GetLogBrush(&logbrush);
  pDC->SetBkColor(logbrush.lbColor);
}

It doesn't work, what's wrong?
I also have a static ctrl on the right pane which I want to change the color in, I need to know how to do that aswell.

Thanks.
0
Comment
Question by:joakimf
5 Comments
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2590593
Try this:

CBrush brush;
brush.CreateSolidBrush (RGB( 128, 128, 128 ));
if ( nCtlColor == CTLCOLOR_BTN )
{
  pDC->SetBkColor(brush);
  return brush;
}

For more details, take a look at Technical Note 62(TN062) in MSDN.

Vicky
0
 
LVL 8

Accepted Solution

by:
VinExpert earned 150 total points
ID: 2590708
Hi,

For that to happen U have set the button style as owner drawn, Then modify the OnCtlColor() as

HBRUSH xxxxx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      CBrush mybrush;
      mybrush.CreateSolidBrush (RGB( 255, 0, 0 ));
      if ( nCtlColor == CTLCOLOR_BTN )
      {
            pDC->SetBkColor(RGB(255,0,0));
            pDC->SetTextColor(RGB(0,0,0));            
            return mybrush;
      }
            
      // TODO: Return a different brush if the default is not desired
      return hbr;
}

But, I dont think U will get the button caption from this method. But the following site gives u full details of hwo to achieve it.

http://codeguru.developer.com/buttonctrl/color_button.shtml

Hope this helps.

VinExpert
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2590752
->CBrush brush;
LOGBRUSH logbrush;
brush.CreateSolidBrush(RGB( 128, 128, 128 ));
if ( nCtlColor == CTLCOLOR_BTN )
{
  brush.GetLogBrush(&logbrush);
  pDC->SetBkColor(logbrush.lbColor);
}
===========
The problem is you must return a brush with wanted color.
Add
return brush;

->I also have a static ctrl on the right pane which I want to change the color in, I need to know how to do that aswell.
====================
check the CTLCOLOR_STATIC:
ie:
ONCTLCOLOR(){
CBrush brush;
brush.CreateSolidBrush (RGB( 128, 128, 128 ));
if ( nCtlColor == CTLCOLOR_STATIC )
{
  pDC->SetBkColor(brush);
  return brush;
}
}
   
Hope helpful




0
 
LVL 1

Author Comment

by:joakimf
ID: 2590803
I got the button working, with the help from code guru, I also wanted help with the static control. I tried Wyns comment, but the SetBkColor doesn't take a CBrush as parameter. So I changed it to this:
CBrush brush;
LOGBRUSH logbrush;
brush.CreateSolidBrush (RGB( 128, 128, 128 ));
if ( nCtlColor == CTLCOLOR_STATIC )
{
      brush.GetLogBrush(&logbrush);
  pDC->SetBkColor(logbrush.lbColor);
  return brush;
}
No luck, any other comments?
0
 
LVL 1

Author Comment

by:joakimf
ID: 2590914
Fixed the static ctrl problem, just put the following code in OnPaint:

CRect rect;
m_staticTitle.GetWindowRect(&rect);
ScreenToClient(&rect);
// Paint the background gray.
CBrush brush;
brush.CreateSolidBrush (RGB( 128, 128, 128 ));
dc.FillRect(rect, &brush);

thanks for your help
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 49
method notes when mouse over in eclipse 5 86
if loop in java 3 140
Way to decrease size of apk file 9 66
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question