CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: "Save me"

  1. #1
    Guest

    "Save me"

    Hello,
    I wanted to change the background color of the "Read Only" Edit control.
    I have tried overriding the "OnCtlColor" but in vain. :-(

    Please help me out. Any valuable suggestion is very much appreciated.
    -Annie


  2. #2
    Guest

    Re: "Save me"

    the following code should work...

    HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    int nID = pWnd->GetDlgCtrlID();
    if( nID == IDC_MYCONTROL )
    {
    // NOTE: m_brush is a CBrush object initialised in contructor of dialog,
    // it must not be a local variable!
    hbr = HBRUSH(m_brush);
    }
    return hbr;
    }



  3. #3
    Join Date
    May 1999
    Posts
    82

    Re: "Save me"

    OK this was taken directly from "Programing Windows with MFC Second Edition" by Jeff Prosise, which I highly recomend.

    First derive your own class from CEdit and use the member variable tab of the class wizard to associate the edit control in your dialog with the new class (double click the edit controls ID and select the new class from the variable type list).

    Next add a member variable of type CBrush in your new class. Then initialized it in the constructor using m_Color.CreateSolidBrush(RGB(0,255,0)).

    Then add

    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);

    to the protected setion of your class, and add

    ON_WM_CTLCOLOR_REFLECT()

    between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP in the .cpp file.
    Finaly add the code for HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); to the .cpp file like so..

    HBRUSH CYourEdit::CtlColor(CDC* pDC, UINT nCtlColor)
    {
    pDC->SetBkColor(RGB(0,255,0));
    return (HBRUSH) m_Color;
    }

    Hope this all makes sense. And I did test the code so I know it works, so if you can't get it to work let me know and I'll try and help you figure out what your doing wrong.

    Dan


  4. #4
    Join Date
    May 1999
    Posts
    82

    Re: "Save me"

    Well I just tried this code and it works as good as mine, but is way simpler to impliment. The only thing that needs to be changed is that you need to call SetBkColor to set the background color to the same color as m_brush or the rectangle just around the text will remain white.

    HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    int nID = pWnd->GetDlgCtrlID();
    if( nID == IDC_MYCONTROL )
    {
    // NOTE: m_brush is a CBrush object initialised in contructor of dialog,
    // it must not be a local variable!
    hbr = HBRUSH(m_brush);
    pDC->SetBkColor(RGB(0,255,0)); // Set to same color as m_brush
    }
    return hbr;
    }


  5. #5
    Guest

    U really did save me!

    Hello Dan and Anonymous,

    Thanks for the help. It works Now I really looooooooooooooove
    CodeGuru.

    Annie


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