I'm using Visual Studio 2008 to write an application for Windows CE 6 using C++ and MFC.
I want to remove the blue highlight of a CComboBox derived class when I've selected an element. 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.
I've tried to use the message CBN_SELCHANGE to send a WM_KILLFOCUS message. 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.
How can I remove the blue highlight of the combo box?
Edit: the combobox is read-only (flag CBS_DROPDOWNLIST)
I've found a (dirty) workaroud, in case nobody give a better approach :
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 :
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:
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.