|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis is my first article. So bear with me. I know it is a simple control but I spent quite a lot of time figuring this out. I hope someone can read this and figure it out quicker. The ProblemI needed a read-only text box. I created one using the The SolutionI derived a class from HBRUSH CReadOnlyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Return a non-NULL brush if the parent's
//handler should not be called
return NULL;
}
To change the background color of the EditBox, instead of returning HBRUSH CReadOnlyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Return a non-NULL brush if the parent's
//handler should not be called
//set text color
pDC->SetTextColor(m_crText);
//set the text's background color
pDC->SetBkColor(m_crBackGnd);
//return the brush used for background this sets control background
return m_brBackGnd;
}
As you can see my control has three variables: void CReadOnlyEdit::SetBackColor(COLORREF rgb) { //set background color ref (used for text's background) m_crBackGnd = rgb; //free brush if (m_brBackGnd.GetSafeHandle()) m_brBackGnd.DeleteObject(); //set brush to new color m_brBackGnd.CreateSolidBrush(rgb); //redraw Invalidate(TRUE); } void CReadOnlyEdit::SetTextColor(COLORREF rgb) { //set text color ref m_crText = rgb; //redraw Invalidate(TRUE); } The reason I have a That's all there was to it. By the way, my control doesn't set a background or text color by default. This can easily be done in the constructor. Using the codeDialog Based:Create a Programmatically:The simplest way I can think of to use this is just change all the Changing the colors:To change the background and text color of the control using the void CReadOnlyDlg::OnBack() { // call color dialog and change background color CColorDialog dlg; if (dlg.DoModal() == IDOK) m_wndReadOnly.SetBackColor(dlg.GetColor()); } Where Note: My control doesn't set the read-only flag, you have to do this yourself in the History
|
||||||||||||||||||||||