Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to change the color of CComboBoxEx

0 views
Skip to first unread message

svilen

unread,
Aug 31, 2003, 12:46:17 PM8/31/03
to
I have derived my class from CComboBoxEx and I change the colors
(SetBkColor and SetTextColor) in OnCtlColor() in response to
CTLCOLOR_EDIT and CTLCOLOR_LISTBOX; I also return my brush;
As a result the colors of the edit part of the combo do change but in
the listbox only the space around the text is painted. The text itself
and its backgound remain as the Windows defaults.

David A. Mair

unread,
Aug 31, 2003, 1:20:47 PM8/31/03
to
I have it working OK, here's my CColorComboBox::OnCtlColor():

HBRUSH CColorComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
LOGBRUSH brush;
HBRUSH hbr;

if ((nCtlColor == CTLCOLOR_LISTBOX) || (nCtlColor == CTLCOLOR_EDIT))
{
// Show white text on a pastel blue background
brush.lbStyle = BS_SOLID;
brush.lbColor = RGB(128, 192, 255);
hbr = CreateBrushIndirect(&brush);
pDC->SetBkColor(RGB(128, 192, 255));
pDC->SetTextColor(RGB(255, 255, 255));
}
else
{
hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
}

return hbr;
}

The project was created with Visual Studio .NET 2003 on Windows 2000
Advanced Server. The project uses a CColorComboBox member in the CDialog
with the member linked to the ID of the combobox control in the dialog.
When I ran the debug target the result was a combobox with a pastel blue
background and when I type in it the text is white. When I drop down the
list (I populated it with a few entries in OnInitDialog()) the result is a
fully filled pastel blue background to the drop down list with the text in
white on a pastel blue background. IOW, the drop down list is completely
coloured with the pastel blue all the way to the black border.

I had the same results as you while testing iterative stages of
developement, it worked as you are seeing until I completed the code to
create my brush.

"svilen" <svi...@hotmail.com> wrote in message
news:f6b84cfe.03083...@posting.google.com...

svilen

unread,
Sep 1, 2003, 2:41:50 PM9/1/03
to
"David A. Mair" <mai...@hotrmail.com> wrote in message news:<ObKF5Q#bDHA...@TK2MSFTNGP12.phx.gbl>...

As I understand your class is derived from CComboBox not from
CComboBoxEx
regarding CComboBox - I have working code and it is generally the same
as yours.
But try deriving your class from CComboBoxEx and then test how the
text background is coloured.
I suspect that the problem lies in the fact that CComboBoxEx
encapsulates an ordinary ComboBox as its child. The WM_CTLCOLOR
message is sent BOTH to the child - the combobox (and this we do not
handle at all) and then to its parent CComboBoxEx (which message we
handle). But notice that CComboBoxEx handler receives the message
second (after the ordinary combobox) but it handles it first, i.e it
is nested in the handler of the combobox. Which means that the
ordinary combobox has the last word and it probably does something
after we change the colors in OnCtlColor

I do not know if my make myself clear but if you can please try
changing your class to be a descendant of CComboBoxEx . not CComboBox
and see the result.

David A. Mair

unread,
Sep 1, 2003, 5:30:55 PM9/1/03
to
"svilen" <svi...@hotmail.com> wrote in message
news:f6b84cfe.03090...@posting.google.com...

Yes, now I can reproduce what you see. However, I did a little research
and, as far as I can see there is no encapsulated CComboBox in CComboBoxEx.
In fact, CComboBoxEx is derived from CComboBox (so a CComboBoxEx is a
CComboBox so doesn't need to contain one and there isn't one in the member
list). For example, here's the guts of the implementation of
CComboBoxEx::GetComboBoxCtrl():

return (CComboBox*) CComboBox::FromHandle((HWND)::SendMessage(m_hWnd,
CBEM_GETCOMBOCONTROL, 0, 0));

IOW, there's a ComboBox inside ComboBoxEx but there isn't a CComboBox inside
CComboBoxEx. I only offer that part for clarity.

This all suggests to me that the problem lies not in the framework but in
the Windows common control implementation for a ComboBoxEx. The only way I
can imagine it being possible to do what you need is to either do an owner
drawn combobox of your own or maybe you can subclass the control. I tried
the latter a few different ways and couldn't get close to resolving the
problem. On searching at Google I find several other reports of the same
problem with others offering their own solutions with comments like: "Extra
formatting properties beyond icon and indentation such as text alignment and
colour are not provided for".


0 new messages