Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1658
  • Last Modified:

CEdit background color

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
nancytan
Asked:
nancytan
  • 5
  • 4
1 Solution
 
psdavisCommented:
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
 
nancytanAuthor Commented:
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
 
psdavisCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nancytanAuthor Commented:
Where do get the pDC then?
0
 
nancytanAuthor Commented:
Where do i get the pDC then?
0
 
psdavisCommented:
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
 
nancytanAuthor Commented:
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
 
psdavisCommented:
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
 
nancytanAuthor Commented:
Thanx, understood.
0
 
thinhCommented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now