简体   繁体   English

Windows CE-禁用CComboBox高亮

[英]Windows CE - disable CComboBox highlight

I'm using Visual Studio 2008 to write an application for Windows CE 6 using C++ and MFC. 我正在使用Visual Studio 2008使用C ++和MFC为Windows CE 6编写应用程序。

I want to remove the blue highlight of a CComboBox derived class when I've selected an element. 选择元素后,我想删除CComboBox派生类的蓝色突出显示。 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. 根据此MSDN文章 ,我无法将组合框的样式设置为LBS_OWNERDRAWFIXED或CBS_OWNERDRAWFIXED来在我的DrawItem函数上选择选择的颜色。

I've tried to use the message CBN_SELCHANGE to send a WM_KILLFOCUS message. 我尝试使用消息CBN_SELCHANGE发送WM_KILLFOCUS消息。 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. 我读过我可以使用paint事件设置突出显示的颜色,但是我不知道或找不到如何执行此操作。

How can I remove the blue highlight of the combo box? 如何删除组合框的蓝色突出显示?

Edit: the combobox is read-only (flag CBS_DROPDOWNLIST) 编辑:组合框是只读的(标志CBS_DROPDOWNLIST)

I've found a (dirty) workaroud, in case nobody give a better approach : 我发现了一个(肮脏的)workaroud,以防没人能提供更好的方法:

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 : 在CComboBox子类头文件中:

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

In your header: 在标题中:

public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

and in cpp: 在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 扩展组合框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM