colors with OnCtlColor

Posted on 1997-12-22
Last Modified: 2013-11-19
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?
Question by:yuqiang

Author Comment

ID: 1312847
Adjusted points to 2

Accepted Solution

richhxb earned 0 total points
ID: 1312848
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating Labels and Frequency list style reports in SAS code 9 105
Define unique primary key 9 95
iSeries DB2 Query 2 97
windows 10 how make picture as desktop background 2 65
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. …
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.
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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