|
-
May 17th, 1999, 12:04 AM
#1
"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
-
May 17th, 1999, 01:54 AM
#2
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;
}
-
May 17th, 1999, 02:19 AM
#3
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
-
May 17th, 1999, 02:31 AM
#4
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;
}
-
May 17th, 1999, 03:30 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|