Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CEdit background color

Posted on 1998-07-16
10
Medium Priority
?
1,654 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 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