Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

colors with OnCtlColor

Since CColorDialog can not used in a propertypage,so I decide to write one myself.I put some CStatic Control on a property page and want to give them different backgroup colors by return different cbrush in OnCtlColor.But I found only 16 colors can be used(eg,RGB(64,64,46) and RGB(192,192,192) are the same ).So I have to draw them myself in the CStatic Control's parent window(the propertypage) in OnPaint.

my question is :why only 16 colors can be used with OnCtlColor?
1 Solution
yuqiangAuthor Commented:
Adjusted points to 2
To use more than only 16 colors, you should not use OnCtlColor,
instead you should use CtlColor.

Now I can tell you how to use CtlColor in your application.

Here is an example to creates a control called CYellowEdit.
The control works the same as a regular edit control except
that it displays black text on a yellow background. It would
be easy to add member functions that would allow the
CYellowEdit control to display different colors.

To try this example, do the following steps:

1.Create a new dialog box in an existing application.

2.With your project loaded into Developer Studio, use
ClassWizard to create a new class called CYellowEdit
based on CEdit. Leave the "Add to Component Gallery"
box checked.

3.Add three member variables to your CYellowEdit class.
The first two will be COLORREF variables to hold the
text color and the background color. The third will be
a CBrush object which will hold the brush for painting
the background. The CBrush object allows you to create
the brush once, merely referencing it after that, and
to destroy the brush automatically when the CYellowEdit
control is destroyed.

4.Initialize the member variables by writing the
constructor as follows:

    //Of course, here you can specify any colors you like.
    m_clrText = RGB( 0, 0, 0 );
    m_clrBkgnd = RGB( 225, 225, 0 );
    m_brBkgnd.CreateSolidBrush( m_clrBkgnd );

5.Using ClassWizard, add a handler for the reflected
WM_CTLCOLOR message to your CYellowEdit class. Note
that the equal sign in front of the message name in
the list of messages you can handle indicates that
the message is reflected. This is very important.

ClassWizard adds the following message-map macro and
skeleton function for you:


// Note: other code will be in between....

HBRUSH CYellowEdit::CtlColor(CDC* pDC, UINT nCtlColor)
    // TODO: Change any attributes of the DC here
    // TODO: Return a non-NULL brush if the
    //    parent's handler should not be called
    return NULL;

6.Just replace the body of the above function with
the following code. The code specifies the text color,
the text background color, and the background color
for rest of the control.

    pDC->SetTextColor( m_clrText );     // text
    pDC->SetBkColor( m_clrBkgnd );    // text bkgnd
    return m_brBkgnd;                          // ctl bkgnd

7.Create an edit control in your dialog box, then
attach it to a member variable with "CYellowEdit"
for the variable type. Be sure to include the header
file for the CYellowEdit control in your dialog box's
header file.

8.Build and run your application. The edit control
will have a yellow background.

I hope this example will help you to solve your problem.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now