CComboBox, itemcolor/style

I have a CComboBox in a dialog.
I want to set differentt colors for certain itemtext (or make them bold).
First of all, is there a direct way ?
What I did is I derived a CXComboBox
: CComboBox with owner draw fixed flag
set. then DrawItem of new class is:

void CXComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
      ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX);
      CString strItem((LPCTSTR) lpDrawItemStruct->itemData);
      CDC dc;
      dc.Attach(lpDrawItemStruct->hDC);
      COLORREF crOldTextColor = dc.GetTextColor();
      if(lpDrawItemStruct->itemID == 2)
            crOldTextColor = dc.SetTextColor(RGB(255,0,0));
      dc.DrawText(strItem, &lpDrawItemStruct->rcItem, 0);
      dc.SetTextColor(crOldTextColor);
      dc.Detach();
}


this seem working, but the problem comes
when I use
GetLBText(index, text);

member, it does not give the text correctly ? is there something I am missing ?
LVL 1
hasmetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

abancroftCommented:
Make sure that the CBS_HASSTRINGS style is also set.
0
hasmetAuthor Commented:
Yes, I forgot to mention,if I set
CBS_HASSTRINGS flag, then I dont see
any Item, but GetLBText works fine,
so so what do I do to make
each item visible
if CBS_HASSTRINGS  is set ???
0
hasmetAuthor Commented:
     CString strItem((LPCTSTR) lpDrawItemStruct->itemData);
return an empty string if
if CBS_HASSTRINGS  is set ???
otherwise it return a correct string
but GetLBText fails ????
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

hasmetAuthor Commented:
my resource is :

    COMBOBOX        IDC_COMBO1,20,47,82,123,CBS_DROPDOWNLIST |
                    CBS_OWNERDRAWFIXED | CBS_HASSTRINGS | CBS_UPPERCASE |
                    WS_VSCROLL | WS_TABSTOP
0
hasmetAuthor Commented:
the following works fine but
I dont think this should be the way ??

void CXComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
      ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX);
      CString strItem; //((LPCTSTR) lpDrawItemStruct->itemData);
      GetLBText(lpDrawItemStruct->itemID, strItem);
      CDC dc;
      dc.Attach(lpDrawItemStruct->hDC);
      COLORREF crOldTextColor = dc.GetTextColor();
      if(m_listcolor[lpDrawItemStruct->itemID])
            crOldTextColor = dc.SetTextColor(RGB(255,0,0));
      dc.DrawText(strItem, &lpDrawItemStruct->rcItem, 0);
      dc.SetTextColor(crOldTextColor);
      dc.Detach();
}
0
abancroftCommented:
Nope - seems correct. Here's the MFC blurb on itemData:

Specifies the application-defined 32-bit value associated with the menu item. For a control, this parameter specifies the value last assigned to the list box or combo box by the LB_SETITEMDATA or CB_SETITEMDATA message. If the list box or combo box has the LBS_HASSTRINGS or CBS_HASSTRINGS style, this value is initially zero. Otherwise, this value is initially the value that was passed to the list box or combo box in the lParam parameter of one of the following messages:
CB_ADDSTRING
CB_INSERTSTRING

Note the section "If the list box or combo box has the LBS_HASSTRINGS or CBS_HASSTRINGS style, this value is initially zero" - i.e. no string.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hasmetAuthor Commented:
I will use GetLBText in DrawItem,
thanks for the effort.
Any idea wht my combo box is larger than the resource.
(the items have more space between them)
0
abancroftCommented:
Are you processing WM_MEASUREITEM?

Here's the SDK help:

"If the application creates the combo box by using the CBS_OWNERDRAWFIXED style, the system sends the WM_MEASUREITEM message only once. The dimensions specified by the owner are used for all list items. If the CBS_OWNERDRAWVARIABLE style is used, the system sends a WM_MEASUREITEM message for each list item added to the combo box"
0
hasmetAuthor Commented:
I do,
void CXComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
      lpMeasureItemStruct->itemHeight = 16;      
}
but the combo box is still taller,
any way I may ask this as a seperate question later. thanks.
0
abancroftCommented:
And what happens when you make the size smaller (e.g. 8)?
0
hasmetAuthor Commented:
     lpMeasureItemStruct->itemHeight = 8;

makes items overlap in the droplist.
but the size of combobox
is still big.
to be exact,
in my resource,
I have two comboboxes.
both 82x12, one is ownerdraw(CXComboBox),
other is regular CComboBox.

when I use GetWindowRect,
I get the diffrence of rigt - left
= 164, that means 82 * 2, is good
because resorce is just a half.
but bottom - top = 28 that is
14 equalen of resorce, where it should be 24 to be 12 equalent of resource ???
the funny thing is that if I do
MoveWindow, cy = 24, it does not
make it 24, keeps it at 28 ???
now that may be it is a font problem
or what ???
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.