Change background color of CComboBox [modified]
-
How can I change the background color of my CComboBox , but only in the edit area , not dropdown list ? I have something like :
class CStatusCombo : public CComboBox
and here :
HBRUSH CStatusCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);// TODO: Change any attributes of the DC here // TODO: Return a different brush if the default is not desired switch(nCtlColor) { case CTLCOLOR\_LISTBOX: pDC->SetTextColor(RGB(0, 255, 0)); pDC->SetBkColor(RGB(120,120,120)); return (HBRUSH)(m\_pEditBkBrush->GetSafeHandle()); } return hbr;
}
I change background of the list area , not the edit area of the combo ... I would like to change the color of edit area just like the statusbar color ... it could be soemthing like that ?
modified on Saturday, October 23, 2010 2:03 PM
-
How can I change the background color of my CComboBox , but only in the edit area , not dropdown list ? I have something like :
class CStatusCombo : public CComboBox
and here :
HBRUSH CStatusCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);// TODO: Change any attributes of the DC here // TODO: Return a different brush if the default is not desired switch(nCtlColor) { case CTLCOLOR\_LISTBOX: pDC->SetTextColor(RGB(0, 255, 0)); pDC->SetBkColor(RGB(120,120,120)); return (HBRUSH)(m\_pEditBkBrush->GetSafeHandle()); } return hbr;
}
I change background of the list area , not the edit area of the combo ... I would like to change the color of edit area just like the statusbar color ... it could be soemthing like that ?
modified on Saturday, October 23, 2010 2:03 PM
Doesn't CTLCOLOR_EDIT work?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
-
Doesn't CTLCOLOR_EDIT work?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
No , I try but didn't work ...
-
No , I try but didn't work ...
How about CTLCOLOR_STATIC?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
-
How can I change the background color of my CComboBox , but only in the edit area , not dropdown list ? I have something like :
class CStatusCombo : public CComboBox
and here :
HBRUSH CStatusCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);// TODO: Change any attributes of the DC here // TODO: Return a different brush if the default is not desired switch(nCtlColor) { case CTLCOLOR\_LISTBOX: pDC->SetTextColor(RGB(0, 255, 0)); pDC->SetBkColor(RGB(120,120,120)); return (HBRUSH)(m\_pEditBkBrush->GetSafeHandle()); } return hbr;
}
I change background of the list area , not the edit area of the combo ... I would like to change the color of edit area just like the statusbar color ... it could be soemthing like that ?
modified on Saturday, October 23, 2010 2:03 PM
You have to subclass the edit control of the combo box, since a combo box is a combined control. Here is an article[^] from Microsoft support describing how to do so.
-
You have to subclass the edit control of the combo box, since a combo box is a combined control. Here is an article[^] from Microsoft support describing how to do so.
I do what they said to , I put the change background code , but in vain ... still don't function ...:confused:
-
How about CTLCOLOR_STATIC?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
It was go with CTLCOLOR_EDIT , but only when I type letters into combo control , when not the background color remain white ...
-
It was go with CTLCOLOR_EDIT , but only when I type letters into combo control , when not the background color remain white ...
I just realized that you are hancling the control-color message in the combo box itself, not in its parent. Did you try with ON_WM_CTLCOLOR_REFELECT or ON_WM_CTLCOLOR?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
-
I do what they said to , I put the change background code , but in vain ... still don't function ...:confused:
I've just tried it with an MFC dialog application. It's not perfect because the color change doesn't "stick" when the edit control has the focus but it comes back when you tab away from the combo box. You'd need to do some more work to make it work the way you want when the edit control has the focus, but this should give you a starting point. The class that I derived from CComboBox is named
CColorEditCombo
. In the Visual Studio designer I added aCComboBox
control to the dialog window. With the ClassWizard I added aCComboBox
control variable namedm_ctlComboColorEdit
. Then in the header file for the dialog class I changed the declaration for this variable to the following:CColorEditCombo m_ctlComboColorEdit;
This associates that control with the new
CColorEditCombo
class rather than the parentCComboBox
class. In the class file forCColorEditCombo
I added a handler for theWM_CTLCOLOR
message as follows:HBRUSH CColorEditCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);switch (nCtlColor) { case CTLCOLOR\_EDIT: case CTLCOLOR\_MSGBOX: // Subclass the edit control. if (m\_edit.GetSafeHwnd() == NULL) { m\_edit.SubclassWindow(pWnd->GetSafeHwnd()); } // Set the background color for the edit control. pDC->SetBkColor(RGB(255, 255, 0)); // Recolor the edit control. m\_brush.DeleteObject(); m\_brush.CreateSolidBrush(RGB(255, 255, 0)); hbr = (HBRUSH) m\_brush.GetSafeHandle(); return hbr; case CTLCOLOR\_LISTBOX: // Subclass the listbox control. if (m\_listBox.GetSafeHwnd() == NULL) { m\_listBox.SubclassWindow(pWnd->GetSafeHwnd()); } // Add recoloring actions for the listbox here if desired. } return hbr; }
Note the member variable named
m_brush
. This has to be declared in the header file for theCColorEditCombo
class as aCBrush
object. It should be deleted in the class destructor as well as just before theCreateSolidBrush
statement shown above. If you'd like, I can send you the solution for the very simple MFC dialog application I put together as a test to make sure this procedure works. You'll have to give me your email address and -
I've just tried it with an MFC dialog application. It's not perfect because the color change doesn't "stick" when the edit control has the focus but it comes back when you tab away from the combo box. You'd need to do some more work to make it work the way you want when the edit control has the focus, but this should give you a starting point. The class that I derived from CComboBox is named
CColorEditCombo
. In the Visual Studio designer I added aCComboBox
control to the dialog window. With the ClassWizard I added aCComboBox
control variable namedm_ctlComboColorEdit
. Then in the header file for the dialog class I changed the declaration for this variable to the following:CColorEditCombo m_ctlComboColorEdit;
This associates that control with the new
CColorEditCombo
class rather than the parentCComboBox
class. In the class file forCColorEditCombo
I added a handler for theWM_CTLCOLOR
message as follows:HBRUSH CColorEditCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);switch (nCtlColor) { case CTLCOLOR\_EDIT: case CTLCOLOR\_MSGBOX: // Subclass the edit control. if (m\_edit.GetSafeHwnd() == NULL) { m\_edit.SubclassWindow(pWnd->GetSafeHwnd()); } // Set the background color for the edit control. pDC->SetBkColor(RGB(255, 255, 0)); // Recolor the edit control. m\_brush.DeleteObject(); m\_brush.CreateSolidBrush(RGB(255, 255, 0)); hbr = (HBRUSH) m\_brush.GetSafeHandle(); return hbr; case CTLCOLOR\_LISTBOX: // Subclass the listbox control. if (m\_listBox.GetSafeHwnd() == NULL) { m\_listBox.SubclassWindow(pWnd->GetSafeHwnd()); } // Add recoloring actions for the listbox here if desired. } return hbr; }
Note the member variable named
m_brush
. This has to be declared in the header file for theCColorEditCombo
class as aCBrush
object. It should be deleted in the class destructor as well as just before theCreateSolidBrush
statement shown above. If you'd like, I can send you the solution for the very simple MFC dialog application I put together as a test to make sure this procedure works. You'll have to give me your email address andI want to thank you for your solution , I will try it , and let you know if I did it ! I use VC6 .