CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 1999
    Location
    Oxford, OH
    Posts
    23

    ! Colored Edit Boxes !

    Hi all,

    I've got a simple class that colors the background and text of edit boxes. I'm writing a program in which I want to change those colors dynamically during execution. Here is the class implementation:

    CColoredEdit::CColoredEdit()
    {
    // default colors //
    m_colorText = RGB( 255, 255, 255 );
    m_colorBkgnd = RGB( 70, 85, 120 );
    m_brBkgnd.CreateSolidBrush( m_colorBkgnd );
    }

    CColoredEdit::CColoredEdit(unsigned long colorText, unsigned long colorBkgnd)
    {
    m_colorText = colorText;
    m_colorBkgnd = colorBkgnd;
    m_brBkgnd.CreateSolidBrush( m_colorBkgnd );
    }

    CColoredEdit::~CColoredEdit()
    {
    }

    void CColoredEdit::SetColors(unsigned long colorText, unsigned long colorBkgnd)
    {
    m_colorText = colorText;
    m_colorBkgnd = colorBkgnd;
    }

    BEGIN_MESSAGE_MAP(CColoredEdit, CEdit)
    //{{AFX_MSG_MAP(CColoredEdit)
    ON_WM_CTLCOLOR_REFLECT()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CColoredEdit message handlers

    HBRUSH CColoredEdit::CtlColor(CDC* pDC, UINT nCtlColor)
    {
    pDC -> SetTextColor( m_colorText );
    pDC -> SetBkColor( m_colorBkgnd );

    return m_brBkgnd;
    }




    The SetColors() function shown above does not have the desired effect. Can anyone tell me how to accomplish this??

    BONUS QUESTION: how do I make the text in the edit box bold?

    Thanks for any and all help!
    Regards,
    Chris


  2. #2
    Join Date
    Apr 1999
    Location
    Frankfurt, Germany
    Posts
    113

    Re: ! Colored Edit Boxes !

    Hi,
    the control is not redrawn when You call SetColors. So the control has the color You want it to have but You can only see an older state. Call one of the functions that cause the edit box to redraw (I never know which one to use).
    You can test whether it is the only reason (I can't see another one) in hiding the control behind another window and removing this window then.
    HTH Rudolf


  3. #3
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: ! Colored Edit Boxes !

    I did something very similar to yours except my SetColors looks like:

    void CBlueEdit::SetColors(COLORREF Fg, COLORREF Bg) {
    m_clrText = Fg;
    m_clrBkgnd = Bg;
    if ((HBRUSH)m_brBkgnd)
    m_brBkgnd.DeleteObject();
    m_brBkgnd.CreateSolidBrush(m_clrBkgnd);
    }



    I am not sure if the DeleteObject is necessary, though.

    And I define m_clrText and m_clrBkgnd as COLORREF.


    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  4. #4
    Join Date
    Oct 1999
    Location
    Oxford, OH
    Posts
    23

    Re: ! Colored Edit Boxes !

    Great! Thanks a lot, works like a charm.

    - chris


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured