If this "if" statement is from your real code, then your troubles are there.
Lets first use
PRE
tags and have a look at your "if" statement:
if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_LISTBOX && pWnd->GetDlgCtrlID() == IDC_COLOR_COMBO)
{
}
Now examine the condition block in your "if" statement and keep in mind that the
AND operator (&&) will be evaluated before the
OR operator (||),
because of it's higher priority[
^].
I'm pretty sure that the correct way of writing this "if" statement is this (with additional brackets to guarantee that the
OR operator (||) will be evaluated first):
if ((nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_LISTBOX) && pWnd->GetDlgCtrlID() == IDC_COLOR_COMBO)
{
}
Also consider deriving a class from
CComboBox
and use the reflection of
WM_CTLCOLOR
in order to have your own colored one.
You can subclass even the list control and the edit control parts of the combobox. Check it out
here[
^].
I hope this helps. :)
[Update]
It seems that there is an interesting behavior of
WM_CTLCOLOR
with combobox. Have e look at
this[
^] discussion.
IMHO the best option is deriving your own class from
CComboBox
. It could be really very simple.
Have a look at this demo class that I've created as an example for you:
- Relevant part from
ColorCombo.h:
class CColorCombo : public CComboBox
{
DECLARE_DYNAMIC(CColorCombo)
public:
CColorCombo();
virtual ~CColorCombo();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
- Relevant part from the
ColorCombo.cpp:
IMPLEMENT_DYNAMIC(CColorCombo, CComboBox)
CColorCombo::CColorCombo()
{
}
CColorCombo::~CColorCombo()
{
}
BEGIN_MESSAGE_MAP(CColorCombo, CComboBox)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
HBRUSH CColorCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_LISTBOX)
{
pDC->SetTextColor(RGB(255, 0, 0));
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH) ::CreateSolidBrush(RGB(255, 255, 0));
}
return hbr;
}
Now in your dialog class you can add member for your combo box, include
ColorCombo.h and change the added member type from
CComboBox
to
CColorCombo
- that's it! :)
An alternative approach is subclassing the combo box. See
here[
^] for details, but I still recommend deriving your own class from
CComboBox
as I've already demonstrated this method. IMHO this is the clearest approach. :)
[/Update]