Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have written below code for applying back color for CCombobox in VC++.
I am launching this vc++ application from a c#.net project

.Cpp file
-----------------------------
C++
BEGIN_MESSAGE_MAP(CMCombo, CComboBox)
    //{{AFX_MSG_MAP(CMCombo)
    ON_WM_CTLCOLOR()
    ON_WM_CTLCOLOR_REFLECT()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

CMCombo::CMCombo()
{
    m_Brush = new CBrush(RGB(255, 255, 255));
}

CMCombo::~CMCombo()
{
    if (m_Brush->GetSafeHandle())
    {
        m_Brush->DeleteObject();
    }
}


HBRUSH CMCombo::CtlColor(CDC* pDC, UINT nColor)
{
    pDC->SetBkColor(RGB(0, 0, 0));
    return (HBRUSH) ::CreateSolidBrush(RGB(0,0,0));
}

HBRUSH CMCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    pDC->SetDCBrushColor(RGB(0, 0, 0));
    pDC->SetBkColor(RGB(0, 0, 0));
    m_Brush = new CBrush(RGB(0, 0, 0));
    return (HBRUSH)(m_Brush->GetSafeHandle());
}



.h file
---------------
C++
public:
	CBrush* m_Brush;

protected:
	//{{AFX_MSG(CMCombo)
	afx_msg HBRUSH CtlColor(CDC* pDC, UINT nColor);
	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()




From above code the backcolor of combobox is not changing. But backcolor of combobox dropdown is getting changed to black.

How to change backcolor of edit part of combobox. Am I missing any code here
Posted

1 solution

Which color handler is called depends on the type of the combo box (with static field or with edit field). You must also check inside the handler which part of the control is affected:
// Get brush and set color for colored backgrounds of edit field 
// (not called for static field).
HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    if (nCtlColor == CTLCOLOR_EDIT) // Edit control
    {
        // Return brush here
    }
    return CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
}

// Get brush and set color for colored backgrounds of static text and edit field.
HBRUSH CMyComboBox::CtlColor(CDC* pDC, UINT nCtlColor)
{
    if (nCtlColor == CTLCOLOR_STATIC || nCtlColor == CTLCOLOR_EDIT)
    {
        // Return brush here
    }
    return NULL; // Send message to parent
}
 
Share this answer
 
Comments
Leo Chapiro 30-Sep-15 7:04am    
perfect as usually, Jochen, +5! Why are you still not MVP (just out of out of curiosity)? How can somebody (e.g. me) recommend you to become one?
Jochen Arndt 30-Sep-15 7:10am    
Thank you.

Most questions are about C# which I don't know. Therefore I'm answering less questions than others here.
Beginner_mfc 28-May-20 12:31pm    
With above code i am able to change bg color of CBS_DROPDOWN type combobox but not of CBS_DROPDOWNLIST type combobox .please tell
yash35 1-Oct-15 9:30am    
Hi Jochen,

I have added below code. The background color is reflecting only in the static field of the drop down list, but in the edit field the background color is not reflecting.

I am not sure where i am missing... I am CCombobox of type Drop Down List where the display field is not editable

The same is code is working fine if i launch this c++ project from a VB application. But if i launch this c++ code from c# application the combobox edit part is not getting colored.

protected:
//{{AFX_MSG(CMCombo)
afx_msg void OnSetFocus( CWnd* pOldWnd );
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nColor);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()




BEGIN_MESSAGE_MAP(CMCombo, CComboBox)
//{{AFX_MSG_MAP(CMCombo)
ON_WM_SETFOCUS()
ON_WM_CTLCOLOR()
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CMCombo::CMCombo()
{
m_pEditBkBrush = new CBrush(RGB(255, 255, 255));
}

CMCombo::~CMCombo()
{
if (m_pEditBkBrush->GetSafeHandle())
{
m_pEditBkBrush->DeleteObject();
}
}

BOOL CMCombo::Create(LPRECT lprect, CWnd* pParentWnd, UINT nID, DWORD dwStyle)
{
CRect rect(lprect);
return CComboBox::Create(dwStyle, rect, pParentWnd, nID);
}

void CMCombo::OnSetFocus( CWnd* pOldWnd )
{
CComboBox::OnSetFocus(pOldWnd);
GetParent()->SendMessage(WM_CHILDGOTFOCUS, (WPARAM)GetSafeHwnd(), 0);
}

HBRUSH CMCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
pDC->SetDCBrushColor(33023);
pDC->SetBkColor(33023);
m_pEditBkBrush = new CBrush(33023);
return (HBRUSH)(m_pEditBkBrush->GetSafeHandle());
}

HBRUSH CMCombo::CtlColor(CDC* pDC, UINT nColor)
{
pDC->SetBkColor(33023);
return (HBRUSH) ::CreateSolidBrush(33023);
}
Jochen Arndt 1-Oct-15 10:07am    
So you have a drop down list where the always visible 'edit' field is a static control. Then only the CtlColor handler is required.

I had used such code in one of my old apps and just checked it. But there is something that must be observed which I forgot to mention in my answer:

When changing the selection, the content in the static input field is drawn as selected by the system (blue background). This will always override the colors passed by your code. If the size of the control is large enough, the selection colours will not be used for the full control size so that there is a border around using the brush returned by CtlColor. But that happens only with disabled themes (classic style). With enabled themes, the selection colours will be used for the complete content.

I have no simple solution to change the color (with enabled themes or for the complete content). The problem is that special effect colours (hovering, highlighting, selections) are drawn by the system as last step. The only solution to avoid this is to draw the control yourself (owner draw). While I have done that for buttons and list controls I did not so for combo boxes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900