Solved

CEdit background color

Posted on 1998-07-16
10
1,639 Views
Last Modified: 2013-11-20
VC5.

How do i programmatically change the backgound color of a CEdit as in

m_pMyEdit->SetBackgroundColor(RGB(0,0,255));

where
 
void CMyEdit::SetBackgroundColor(COLORREF cr)
{
// ???
}

Thank.
0
Comment
Question by:nancytan
  • 5
  • 4
10 Comments
 
LVL 7

Expert Comment

by:psdavis
ID: 1319355
Check out topics based on message reflection.

Here's a tech note example

Phillip...


Handling Reflected Messages: An Example of a Reusable control

This simple example creates a reusable 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:

Create a new dialog box in an existing application. For more information see dialog editor in the Developer Studio User's Guide.
You must have an application in which to develop the reusable control. If you don't have an existing application to use, create a dialog-based application using AppWizard.

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.
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.
Initialize the member variables by writing the constructor as follows:



CYellowEdit::CYellowEdit()
{
    m_clrText = RGB( 0, 0, 0 );
    m_clrBkgnd = RGB( 255, 255, 0 );
    m_brBkgnd.CreateSolidBrush( m_clrBkgnd );
}
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 described in Defining a Message Handler for a Reflected Message in the Visual C++ Programmer's Guide.
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;
}
Replace the body of the 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
Create an edit control in your dialog box, then attach it to a member variable by double-clicking the edit control while holding a control key down. In the Add Member Variable dialog box, finish the variable name and choose "Control" for the category, then "CYellowEdit" for the variable type. Don't forget to set the tab order in the dialog box. Also, be sure to include the header file for the CYellowEdit control in your dialog box's header file.
Build and run your application. The edit control will have a yellow background.
You can now use Component Gallery to add your CYellowEdit control class to other projects.

0
 

Author Comment

by:nancytan
ID: 1319356
Thanks psdavis.

But i need to change the color from outside the CEdit class.
for example.
 
void CMyDlg::DoThis()
{
 m_pMyEdit->SetBackgroundColor(RGB(0,0,255));

 //do something else
 m_pMyEdit->SetBackgroundColor(RGB(0,225,0));

 //do something else
 m_pMyEdit->SetBackgroundColor(RGB(225,0,0));

}
0
 
LVL 7

Expert Comment

by:psdavis
ID: 1319357
Still the way to do it.

If you want it to outside users, then you want to add a function member that accepts a RGBQUAD as a parameter, then you replace the m_clrBkgrd member variable with it.

Phillip

0
 

Author Comment

by:nancytan
ID: 1319358
Where do get the pDC then?
0
 

Author Comment

by:nancytan
ID: 1319359
Where do i get the pDC then?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Expert Comment

by:psdavis
ID: 1319360
The pDC is given to you from the CtlColor function, you don't need to create it yourself.  You can check out the entire tech note for more details than I've already included.  It's actually quite large.

The code to do the TechNote really isn't that large or complex.  Once it's done, you can reuse it quite easily.

Signing off for the night.  If you have more questions, I'll have to send them at 9 CST.  Phillip

0
 

Author Comment

by:nancytan
ID: 1319361
I have read through that technote.
I still could not figure how to get the pDC required.

void CMyEdit::SetBackgroundColor(COLORREF cr)
{

 // how to get the pDC in this function ?
 // or am i doing the wrong way?
 // Sample code appreciate

  m_clrBkgnd = cr ;
  pDC->SetBkColor( m_clrBkgnd );    // text bkgnd
 
}

Sorry, I could not wait until 9 CST.
0
 
LVL 7

Accepted Solution

by:
psdavis earned 20 total points
ID: 1319362
You don't need to send the pDC the SetBkColor, the next WM_CTLCOLOR message will do it automatically.  Just make sure you do your SetBackgroundColor in the InitDialog section of your dialog so everything will be updated the first time.

Phillip

0
 

Author Comment

by:nancytan
ID: 1319363
Thanx, understood.
0
 

Expert Comment

by:thinh
ID: 1319364
U can change the background color of CEdit control on this way
//editBrush is a CBrush member


OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    switch(nCtlColor)
    {
      case CTLCOLOR_EDIT:      
            pDC->SetBkMode( TRANSPARENT );
            return static_cast<HBRUSH(editBrush.GetSafeHandle());
    }
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 47
sum13 challenge 24 77
java ^ examples 8 57
pre4 challenge 19 88
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

12 Experts available now in Live!

Get 1:1 Help Now