2017-06-15 68 views
1

我正在使用Visual Studio 2008为使用C++和MFC的Windows CE 6编写应用程序。Windows CE - 禁用CComboBox突出显示

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

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

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

如何删除组合框的蓝色突出显示?组合框是只读的(标志CBS_DROPDOWNLIST)

+0

在这里你会找到的WinForms解决方案:https://www.experts-exchange.com/questions/25070779 /How-do-I-remove-avoid-the-blue-selection-color-in-a-combo-box.html,基本上为了响应CBN_SELCHANGE,你应该发布一条消息(或者启动一个短定时器)并且在回调中函数调用值为(0,0)的CComboBox :: SetCurSel。我不确定这是否会按预期工作。 – marcinj

+0

我在OnCbnSelchange中放置了一个定时器(1ms和500ms超时),并且在定时器触发时调用SetCurSel(0) - 只有1个参数。它清空组合框(索引0上没有文本),但让组合框以蓝色突出显示... – Ayak973

+0

对不起,正确的功能应该是SetEditSel https://msdn.microsoft.com/en-us/library/12h9x0ch .aspx#ccombobox__seteditsel – marcinj

回答

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

在您的标题:

编辑

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

+0

正如我在我的问题中说的,你不能使用Windows CE 6的所有者绘制的组合框。重载的函数DrawItem不会被调用。 – Ayak973