Solved

colors with OnCtlColor

Posted on 1997-12-22
2
346 Views
Last Modified: 2013-11-19
Hi,all,
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?
0
Comment
Question by:yuqiang
2 Comments
 

Author Comment

by:yuqiang
ID: 1312847
Adjusted points to 2
0
 
LVL 1

Accepted Solution

by:
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:

CYellowEdit::CYellowEdit()
{
    //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:

ON_WM_CTLCOLOR_REFLECT()

// 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.


0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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