Solved

CEdit background color

Posted on 1998-07-16
10
1,645 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 101
sum67 challenge 35 110
either24  challenge 19 101
matchUp  challenge 6 70
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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