Solved

Change color of controls.

Posted on 2000-03-06
5
1,064 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
[X]
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
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

623 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