Click to See Complete Forum and Search --> : "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
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;
}
Dan Haddix
May 17th, 1999, 02:19 AM
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
Dan Haddix
May 17th, 1999, 02:31 AM
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;
}
Hello Dan and Anonymous,
Thanks for the help. It works :) Now I really looooooooooooooove
CodeGuru.
Annie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.