Solved

colors with OnCtlColor

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

17 Experts available now in Live!

Get 1:1 Help Now