Windows CE - 禁用CComboBox突出显示

时间:2017-06-15 12:21:01

标签: c++ mfc windows-ce ccombobox

我使用Visual Studio 2008使用C ++和MFC为Windows CE 6编写应用程序。

我想在选择元素时删除CComboBox派生类的蓝色突出显示。 根据{{​​3}},我无法将组合框的样式设置为LBS_OWNERDRAWFIXED或CBS_OWNERDRAWFIXED以选择我的DrawItem函数上的选择颜色。

我尝试使用CBN_SELCHANGE消息发送WM_KILLFOCUS消息。部分工作:控件失去焦点(选中的元素不再是蓝色),但是如果我再次单击组合框,它就不会显示元素列表。

我已经读过我可以使用绘画事件来设置突出显示的颜色,但我不知道或不知道如何做到这一点。

如何删除组合框的蓝色突出显示?

编辑:组合框是只读的(标志CBS_DROPDOWNLIST)

2 个答案:

答案 0 :(得分:0)

我找到了一个(肮脏的)workaroud,万一没有人给出更好的方法:

我在创建组合框时设置了父级:

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

当我使用组合框完成时,以下行将焦点放在父元素上。

在CComboBox子类头文件中:

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

在源文件中:

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();
    }
}

答案 1 :(得分:-1)

在标题中:

public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

和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);

}

模型取自这里:

Extended combobox