Custom CEdit Text Control
Ok I spent over 24 hours on groups just trying to sub class an MFC
CEdit control. All the information I found was correct but I still
had to work out a good 30% of the solution myself. Bits of vital -
assumed - information was missing.
I am posting this in an attempt to stop anyone else having to spend so
much time to perform such a simple task.
First forget SubClassDlgItem(). This can be used if required, but
just obfuscate the code.
Ill highlight points of interest and gotchas I found!!!!!
Here is the source:
This is the custom class .h file which inherits from CEdit.
////////////////////////////////////////////////////////////////////////////////////
#pragma once
// _CustomEditCTRL
class _CustomEditCTRL : public CEdit
{
DECLARE_DYNAMIC(_CustomEditCTRL) // Changed to match class
public:
_CustomEditCTRL();
virtual ~_CustomEditCTRL();
// Added manually.
COLORREF m_crText;
COLORREF m_crBackGnd;
CBrush m_brBackGnd;
// Add as mere access
functions. // Added manually.
void _CustomEditCTRL::SetBackColor(COLORREF); // Must be public to
call.
void _CustomEditCTRL::SetTextColor(COLORREF);
protected:
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); // Added
manually.
DECLARE_MESSAGE_MAP()
};
This is the custom class cpp file which inherits from CEdit.
////////////////////////////////////////////////////////////////////////////////////
// _CustomEditCTRL
IMPLEMENT_DYNAMIC(_CustomEditCTRL, CEdit)
_CustomEditCTRL::_CustomEditCTRL()
{
// Added manually.
// Set the custom control member variables.
MessageBoxA(NULL,"_CustomEditCTRL","",MB_OK);
m_crText=RGB(100,0,0);
}
_CustomEditCTRL::~_CustomEditCTRL()
{
}
// Added manually.
HBRUSH _CustomEditCTRL::CtlColor(CDC* pDC, UINT nCtlColor)
{
MessageBoxA(NULL,"CtlColor","",MB_OK); // Checks that this is
called indirectly
//
never called directly.
//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 (HBRUSH) m_brBackGnd.GetSafeHandle();
}
// Added manually.
void _CustomEditCTRL::SetBackColor(COLORREF rgb)
{
MessageBoxA(NULL,"SetBackColor","",MB_OK); // Checks
that this is called
//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);
}
// Added manually.
void _CustomEditCTRL::SetTextColor(COLORREF rgb){
//set text color ref
m_crText = rgb;
//
GOTCHA - added to create background also...can remove.
SetBackColor(COLORREF(RGB(255,255,255))); // <- You MUST specify a
brush fo 4 the control to work!
//
If not - no change seems to happen.
//redraw
Invalidate(TRUE);
}
BEGIN_MESSAGE_MAP(_CustomEditCTRL, CEdit) // Messages passed
around...
ON_WM_CTLCOLOR_REFLECT() // GOTCHA - ON_WM_CTLCOLOR will not
work use this
END_MESSAGE_MAP() //
instead. // Added manually.
OK we now have a custom class which inherits from a CEdit control...
How do we call it?
firstly you need to create a member variable for an existing CEdit
control which will utilise this custom class.
// So in the parent class i.e. the class which hosts the control add
this.
_CustomEditCTRL m_statusCtrl;
// to call this - do this:
m_statusCtrl.SetTextColor(COLORREF(RGB(200,0,0)));
NOTE: Remember this also calls the background colour function (Cos I
told it to as i dont want to change this all the time I want to change
the text)...just comment out the
SetBackColor(COLORREF(RGB(255,255,255))); BUT specify a default brush
after.
OK - Rember to add the header file for the _CustomEditCTRL i.e.
#include"_CustomEditCTRL .h" where ever you use m_statusCtrl and on
the parent of the control.
Additional notes:
This was tested on vista OK
Visual Studios 2005 is pants - No Class Wizards used.
PLEASE: This is not a "How great am I" post - This is to try and stop
the time wasted I spend trying to get it going....PLEASE comment!!!!
This was developed for CPropertyPage with the parent a CPropertySheet
(NO Dialog) so maybe the ON_WM_CTLCOLOR windows message would have
worked for CDialog parents. I have found some strange posts on Dialog
requiring the InitDialog function to be overridden before they recieve
and process messages...lol
Anyway...If I can help more...pls ask!!!!
If I should remove my code as a disgrace to the dark art of MFC then
also comment.