880410 - CtlColor
-
hi i need an edit ctrl for entering numerical values. sometimes these values must lie in a specified range. otherwise, i want to warn the user by changing the edit-box color. i derived a class from CEdit for this purpose and tried to put the code in the CtlColor:
HBRUSH MyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (d_chars==decimalChars) // if mode==emDecimal
{
CString s;
GetWindowText(s);
USES_CONVERSION;
int val=atoi(T2A(s));
if (val<d_minVal || val>d_maxVal)
{
pDC->SetTextColor(RED);
pDC->SetBkColor(YELLOW);
}
}
return NULL;
}i don't need to change the default brush used for painting the ctrl, so i return NULL. but this doesn't work. i tried returning a valid handle to a brush i created, but i could at most change the background color, not the text color. what's the problem? what's the best solution? thx
-
hi i need an edit ctrl for entering numerical values. sometimes these values must lie in a specified range. otherwise, i want to warn the user by changing the edit-box color. i derived a class from CEdit for this purpose and tried to put the code in the CtlColor:
HBRUSH MyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (d_chars==decimalChars) // if mode==emDecimal
{
CString s;
GetWindowText(s);
USES_CONVERSION;
int val=atoi(T2A(s));
if (val<d_minVal || val>d_maxVal)
{
pDC->SetTextColor(RED);
pDC->SetBkColor(YELLOW);
}
}
return NULL;
}i don't need to change the default brush used for painting the ctrl, so i return NULL. but this doesn't work. i tried returning a valid handle to a brush i created, but i could at most change the background color, not the text color. what's the problem? what's the best solution? thx
ilostmyid2 wrote:
derived a class from CEdit for this purpose
This is not correct.
WM_CTLCOLOR
is sent to the parent window. So you should be handlingOnCtlColor
of the parent dialog. In that, check if the message is for your particular edit control and then dopDC->SetTextColor
.«_Superman_» I love work. It gives me something to do between weekends.
-
hi i need an edit ctrl for entering numerical values. sometimes these values must lie in a specified range. otherwise, i want to warn the user by changing the edit-box color. i derived a class from CEdit for this purpose and tried to put the code in the CtlColor:
HBRUSH MyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (d_chars==decimalChars) // if mode==emDecimal
{
CString s;
GetWindowText(s);
USES_CONVERSION;
int val=atoi(T2A(s));
if (val<d_minVal || val>d_maxVal)
{
pDC->SetTextColor(RED);
pDC->SetBkColor(YELLOW);
}
}
return NULL;
}i don't need to change the default brush used for painting the ctrl, so i return NULL. but this doesn't work. i tried returning a valid handle to a brush i created, but i could at most change the background color, not the text color. what's the problem? what's the best solution? thx
MyEditBox::MyEditBox()
{
m_brush = NULL;
itemData = NULL;
}MyEditBox::~MyEditBox()
{
delete m_brush;
}BEGIN_MESSAGE_MAP(MyEditBox, CEdit)
//{{AFX_MSG_MAP(MyEditBox)
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()bool MyEditBox::SetColorScheme(COLORREF brushColor, COLORREF bgTextColor, COLORREF fgTextColor)
{
delete m_brush;
m_brush = new CBrush(brushColor);
bgc = bgTextColor;
fgc = fgTextColor;
Invalidate();
UpdateWindow();
return (m_brush != NULL);
}void MyEditBox::ResetColorScheme()
{
delete m_brush;
m_brush = NULL;
Invalidate();
UpdateWindow();
}
/////////////////////////////////////////////////////////////////////////////
// MyEditBox message handlers
/////////////////////////////////////////////////////////////////////////////HBRUSH MyEditBox::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (m_brush == NULL)
return NULL;
//note - if brush and text background color are different, anomalies will occur
pDC->SetTextColor(fgc);
pDC->SetBkColor(bgc);
return (HBRUSH)m_brush->GetSafeHandle();
}