Solved

Change color of controls.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 87
Detect CR LF to each line 12 137
zeroFront challenge 7 71
Unix Command -- Challenging  question 7 79
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now