[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 660
  • Last Modified:

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 ?
0
hasmet
Asked:
hasmet
  • 7
  • 4
1 Solution
 
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now