Solved

Change color of controls.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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…

735 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