Solved

CEdit background color

Posted on 1998-07-16
10
1,641 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
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.

 
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

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
Add content to output file 4 62
Tab names are off by one 5 53
sum28 challenge 31 105
twoTwo  challenge 35 92
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 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

14 Experts available now in Live!

Get 1:1 Help Now