Solved

colors with OnCtlColor

Posted on 1997-12-22
2
344 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 92
while loop over for loop 7 92
post4 challenge 28 91
fix34  challenge 9 104
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

947 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

20 Experts available now in Live!

Get 1:1 Help Now