Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Change color of controls.

Posted on 2000-03-06
5
1,049 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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