1

I'm using Visual Studio 2008 to write an application for Windows CE 6 using C++ and MFC.

I want to remove the blue highlight of a CComboBox derived class when I've selected an element. According to this MSDN article, I cannot set the style of the combo box to LBS_OWNERDRAWFIXED or CBS_OWNERDRAWFIXED to choose the color of the selection on my DrawItem function.

I've tried to use the message CBN_SELCHANGE to send a WM_KILLFOCUS message. It partially work : the control loose its focus (the selected element is not blue anymore), but if I click again the combo box, it didnt show the list of elements.

I've read that I can use the paint event to set the color of the highlight, but I didn't know or find how to do this.

How can I remove the blue highlight of the combo box?

Edit: the combobox is read-only (flag CBS_DROPDOWNLIST)

Ayak973
  • 468
  • 1
  • 6
  • 23
  • Here you will find solution for winforms: https://www.experts-exchange.com/questions/25070779/How-do-I-remove-avoid-the-blue-selection-color-in-a-combo-box.html, basicly in response to CBN_SELCHANGE you should post a message (or start a short timer) and in the callback function call CComboBox::SetCurSel with values of (0, 0). I'am not sure if this is going to work as expected. – marcinj Jun 15 '17 at 12:29
  • I've put a timer (with 1ms and 500ms timeout) inside OnCbnSelchange, and call SetCurSel(0) - wich has only 1 argument - when the timer fires. It empty the combo box (it has no text on index 0), but let the combo highlighted in blue... – Ayak973 Jun 15 '17 at 12:46
  • Sorry the correct function should be SetEditSel https://msdn.microsoft.com/en-us/library/12h9x0ch.aspx#ccombobox__seteditsel – marcinj Jun 15 '17 at 13:07
  • @marcinj : thank you for the tip, I've tested it with different arguments (according to the documentation, 0,0/-1,0) and cannot make it work, even with a timer...Is this because the control is not editable (flag CBS_DROPDOWNLIST)? – Ayak973 Jun 15 '17 at 15:17
  • Have you tried resetting the cur sel in the CBN_SELENDOK handler? – Michaël Roy Jun 29 '17 at 15:20
  • I've read the MSDN documentation for SetEditSel, and if i'm right it cannot work due to the flag CBS_DROPDOWNLIST (the function return CB_ERR) – Ayak973 Jun 30 '17 at 12:29

2 Answers2

0

I've found a (dirty) workaroud, in case nobody give a better approach :

I set a parent when I create the combobox :

customCombo.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWNLIST | CBS_DROPDOWN, CRect(0, 0, 0, 0), **PARENT**, COMBO_ID);

The following lines give the focus to the parent element when I'm done using the combo box.

In CComboBox subclass header file :

public:
    afx_msg void OnCbnSelchange();
    afx_msg void OnCbnSelendcancel();
    afx_msg void OnCbnSelendok();

In source file :

void CustomCombo::OnCbnSelchange() {
    //give focus to parent
    CWnd* cwnd = GetParent();
    if (cwnd != NULL) {
        cwnd->SetFocus();
    }
}


void CustomCombo::OnCbnSelendcancel() {
    //give focus to parent
    CWnd* cwnd = GetParent();
    if (cwnd != NULL) {
        cwnd->SetFocus();
    }
}

void CustomCombo::OnCbnSelendok() {
    //give focus to parent
    CWnd* cwnd = GetParent();
    if (cwnd != NULL) {
        cwnd->SetFocus();
    }
}
Ayak973
  • 468
  • 1
  • 6
  • 23
-1

In your header:

public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

and in cpp:

void CYourComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
// TODO: Add your code to draw the specified item
CDC* pDC = CDC::FromHandle (lpDrawItemStruct->hDC);

if (((LONG)(lpDrawItemStruct->itemID) >= 0) &&
    (lpDrawItemStruct->itemAction & (ODA_DRAWENTIRE | ODA_SELECT)))
{
    // color item as you wish
}

if ((lpDrawItemStruct->itemAction & ODA_FOCUS) != 0)
    pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);

}

the model are taken from here:

Extended combobox

Flaviu_
  • 1,285
  • 17
  • 33
  • As I said in my question, you cannot use owner drawn combo box for Windows CE 6. The overloaded function DrawItem is never called. – Ayak973 Jun 30 '17 at 12:18