1

我正在使用 Visual Studio 2008 使用 C++ 和 MFC 编写适用于 Windows CE 6 的应用程序。

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

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

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

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

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

4

2 回答 2

0

我找到了一个(肮脏的)工作方法,以防没有人提供更好的方法:

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

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();
    }
}
于 2017-07-24T08:51:41.157 回答
-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);

}

模型取自这里:

扩展组合框

于 2017-06-30T08:39:06.693 回答