2017-06-15 64 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