How to change the background color of an edit box 
Author Message
 How to change the background color of an edit box

I have an edit box that I have made read-only. Visual C++ then
automatically makes the background of the edit box gray. I want to have
it white however. How can I change the background color to white?
A code sample please, if possible.
Thanx


Mon, 26 Nov 2001 03:00:00 GMT  
 How to change the background color of an edit box

Quote:
> I have an edit box that I have made read-only. Visual C++ then
> automatically makes the background of the edit box gray. I want to have
> it white however. How can I change the background color to white?

Subclass the edit control.  
Handle WM_CTLCOLOR

  1) Call SetTextColor if you want to change the text color.
  2) Call SetBkColor
  3) Return a brush with the same color as SetBkColor

Don't create the brush in your WM_CTLCOLOR handler. Make it a member of your
dialog and create it in the dialog's InitDialog.  (WM_CTLCOLOR gets lots of
calls, so you don't want to be creating a brush every time it gets called.)

Jim [VC/MFC MVP]
To send mail, change spam-me-not to msn



Mon, 26 Nov 2001 03:00:00 GMT  
 How to change the background color of an edit box

I had the same problem and got around it by creating a custom property, ReadOnly, and then overriding some event handlers:

void CTextboxCtrl::OnSetFocus(CWnd* pOldWnd)
{
        if (m_ReadOnly == 0)
                COleControl::OnSetFocus(pOldWnd);
        InvalidateControl();

Quote:
}

void CTextboxCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
        if (m_ReadOnly == 0)
                COleControl::OnKeyDown(nChar, nRepCnt, nFlags);
        InvalidateControl();

Quote:
}

void CTextboxCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
        if (m_ReadOnly == 0)
                COleControl::OnChar(nChar, nRepCnt, nFlags);
        InvalidateControl();

Quote:
}

The resulting control does not receive the focus or respond to key presses unless the ReadOnly property is set to 0.


Quote:
>I have an edit box that I have made read-only. Visual C++ then
>automatically makes the background of the edit box gray. I want to have
>it white however. How can I change the background color to white?
>A code sample please, if possible.
>Thanx

-----------------** -- Posted from CodeGuru -- **-----------------
http://www.codeguru.com/    The website for Visual C++ programmers.


Mon, 26 Nov 2001 03:00:00 GMT  
 How to change the background color of an edit box

To change the background color you must handle the reflected
message:

LRESULT CTextboxCtrl::OnOcmCtlColorEdit(WPARAM wParam, LPARAM lParam)
{
#ifdef _WIN32
        WORD wNotifyCode = HIWORD(wParam);
#else
        WORD wNotifyCode = HIWORD(lParam);
#endif

        HBRUSH hNewBrush = ::CreateSolidBrush(TranslateColor (m_BrushColorIndex, NULL));  
        m_WhiteBrush.Attach(hNewBrush);
        ::SetBkColor((HDC)wParam, TranslateColor (m_BrushColorIndex, NULL));
        ::SelectObject((HDC)wParam, (HBRUSH)m_WhiteBrush);
        ::SetTextColor((HDC)wParam, TranslateColor (m_TextColorIndex, NULL));
        return 0;

Quote:
}

This code is from an ActiveX control the subclasses CEdit. It
works great with Windows NT but not with Windows 95. So far,
I have not found a way to change the background color in
Windows 95. If anyone knows how to do this please let me know.


Quote:
>I have an edit box that I have made read-only. Visual C++ then
>automatically makes the background of the edit box gray. I want to have
>it white however. How can I change the background color to white?
>A code sample please, if possible.
>Thanx

-----------------** -- Posted from CodeGuru -- **-----------------
http://www.codeguru.com/    The website for Visual C++ programmers.


Mon, 26 Nov 2001 03:00:00 GMT  
 How to change the background color of an edit box
You have to handle the WM_CTLCOLOR message. This message is sent to all the
controls. If you handle it in your OnCtlColor function and return HBRUSH
variable then this would be used for the control (this message is sent for
all the controls and so you have to check the parameter to figure out if it
is for the control you want to change).

Hope it helps. I can't send you a real code because I am not on my computer
and can't try it.

David Pokluda

PS: You can look in David Kruglinksi's Inside Visual C++ book. I remember
there was something like that.



Mon, 26 Nov 2001 03:00:00 GMT  
 How to change the background color of an edit box

Quote:

> LRESULT CTextboxCtrl::OnOcmCtlColorEdit(WPARAM wParam, LPARAM lParam)
> {
>    HBRUSH hNewBrush = ::CreateSolidBrush(TranslateColor (m_BrushColorIndex, NULL));  
>    m_WhiteBrush.Attach(hNewBrush);
>    ::SetBkColor((HDC)wParam, TranslateColor (m_BrushColorIndex, NULL));
>    ::SelectObject((HDC)wParam, (HBRUSH)m_WhiteBrush);
>    ::SetTextColor((HDC)wParam, TranslateColor (m_TextColorIndex, NULL));
>    return 0;
> }

Joe,

Have you had any problems with this control?  The way the code is, you'll be
creating a new brush every time the function is called, which can be a bunch
of times. I've always made the brush a member of the class and initialized it
when the control is created, then just return the class member each time.

Jim [VC/MFC MVP]
To send mail, change spam-me-not to msn



Mon, 26 Nov 2001 03:00:00 GMT  
 How to change the background color of an edit box
Take a look at following MSDN article:

HOWTO: Change the Background Color of an MFC Edit Control
Last reviewed: November 3, 1998
Article ID: Q117778

--
Ajay Kalra


Quote:
>I have an edit box that I have made read-only. Visual C++ then
>automatically makes the background of the edit box gray. I want to have
>it white however. How can I change the background color to white?
>A code sample please, if possible.
>Thanx



Mon, 26 Nov 2001 03:00:00 GMT  
 How to change the background color of an edit box

Jim,
I was hoping you would have a comment. When I originally built
that activex control I created a white, black, red, green and
blue CBrush in the constructor and then deleted them in the
destructor. This was fine except that I did not want to be
restricted to those colors. Now I can set my brushcolor property
(OLE_COLOR) to 0xff0000 or whatever and get any color. I agree
that it is not a good idea to keep creating HBRUSH's  over and
over. But I'm still looking for the documentation to find out
exactly why or at least find a problem with it. For example,
what happens to the HBRUSH when the OnOcmCtlColorEdit function
is exited? Do the HBRUSH's stay in memory and eventually cause
the computer to lock up? I was hoping the HBRUSH's would just go
away when the function exits. Maybe I could try deleting the  
HBRUSH after it is attached to the CBrush. So far I haven't
noticed any bad effects.

However, there are bigger problems with my activex control. I
use the COleControl function RecreateControlWindow( ) to call
PreCreateWindow() so that I can change the text alignment
in the edit box:

BOOL CTextboxCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
        cs.lpszClass = _T("EDIT");

        if ((m_WindowStyle >= 0) && (m_WindowStyle <= 2 ))
        {
                switch (m_WindowStyle)
                {
                case 0:
                        cs.style |= ES_LEFT | WS_TABSTOP | ES_AUTOHSCROLL;
                        break;
                case 1:
                        cs.style |= ES_RIGHT | WS_TABSTOP | ES_AUTOHSCROLL;
                        break;
                case 2:
                        cs.style |= ES_CENTER | WS_TABSTOP | ES_AUTOHSCROLL;
                        break;
                default:
                        cs.style |= ES_LEFT | WS_TABSTOP | ES_AUTOHSCROLL;
                }
        }
        else
                        AfxMessageBox("'TextAlign' valid selections are:\n0 Left Aligned\n1 Right Aligned\n2 Center Aligned");

        if (m_Disable == 1)
                cs.style |= WS_DISABLED;
        else if (m_Disable == 0)
                {}
        else
                AfxMessageBox("'Disable' valid selections are:\n0 Disabled\n1 Enabled");

        return COleControl::PreCreateWindow(cs);

Quote:
}

This works fine in Windows NT. In Windows 95 this code has no
effect on the edit box and niether does the code for changing
the background color. Do you have any idea what's going on here?
I am using this edit control in an HTML page. Originally I used
the textbox contained in FM20.DLL but then I found out the dll
is not redistributable so I decided to make my own control. The
big picture here is that I designed a form using html and am
displaying it in a dialog based container application with a
webbrowser control. Communication between the container and the
html page is done with COM via an invisible activex control in
the html page and by programming the DOM. And another thing,
FM20.DLL was distributed with IE3 but is not being distributed
with IE4.

Thanks,
Joe.

Quote:


>> LRESULT CTextboxCtrl::OnOcmCtlColorEdit(WPARAM wParam, LPARAM lParam)
>> {
>>        HBRUSH hNewBrush = ::CreateSolidBrush(TranslateColor (m_BrushColorIndex, NULL));  
>>        m_WhiteBrush.Attach(hNewBrush);
>>        ::SetBkColor((HDC)wParam, TranslateColor (m_BrushColorIndex, NULL));
>>        ::SelectObject((HDC)wParam, (HBRUSH)m_WhiteBrush);
>>        ::SetTextColor((HDC)wParam, TranslateColor (m_TextColorIndex, NULL));
>>        return 0;
>> }

>Joe,

>Have you had any problems with this control?  The way the code is, you'll be
>creating a new brush every time the function is called, which can be a bunch
>of times. I've always made the brush a member of the class and initialized it
>when the control is created, then just return the class member each time.

>Jim [VC/MFC MVP]
>To send mail, change spam-me-not to msn

-----------------** -- Posted from CodeGuru -- **-----------------
http://www.codeguru.com/    The website for Visual C++ programmers.


Tue, 27 Nov 2001 03:00:00 GMT  
 How to change the background color of an edit box
this article shows exactly what needs to be done
HOWTO: Change the Background Color of an MFC Edit Control
Last reviewed: November 3, 1998
Article ID: Q117778
 http://support.microsoft.com/support/kb/articles/q117/7/78.asp

--
----------------------------------------
Rajesh Parikh

----------------------------------------



Quote:
> I have an edit box that I have made read-only. Visual C++ then
> automatically makes the background of the edit box gray. I want to have
> it white however. How can I change the background color to white?
> A code sample please, if possible.
> Thanx



Mon, 03 Dec 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Change the background color of a dialog box and change text color of Edit Box, Static Text, Rich Edit

2. Edit Box background color

3. color background - edit box

4. Edit box background color control

5. Can't change background color of edit control

6. How to change background color of a disabled edit control

7. Changing background color for disabled edit controls

8. Changing background color of disabled edit controls

9. Changing background color of disabled edit control

10. Changing background color of Edit control in CEditView

11. Changing an edit box's background colour

12. OnCtlColor not changing edit box background

 

 
Powered by phpBB® Forum Software