-
February 11th, 2011, 04:29 AM
#1
color list of ccombo box
Hey
I have a class MyCombo that i derrived from ccombobox ,
When the mouse is over an item in the list of the combo, the background color of the item been colored with blue.
Is it possible to control the the background color of items that the mouse is over them?
thanks
-
February 11th, 2011, 07:17 AM
#2
Re: color list of ccombo box
Yes, it is possible if make an owner-draw combobox (having CBS_OWNERDRAWFIXED or CBS_OWNERDRAWVARIABLE style set).
That case, you can and MUST draw each list item (as your muscles want).
See CComboBox::DrawItem.
-
February 13th, 2011, 02:38 PM
#3
Re: color list of ccombo box
Hey
The code in the DrawItem Is not helping me .
code:
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
I want to catch the event of mouse over an item and not the mouse select on an item .
-
February 13th, 2011, 05:44 PM
#4
Re: color list of ccombo box
Never mind - i find a way to do onMouseMove and highlight items in the list :
Code:
BEGIN_MESSAGE_MAP(CMyList, CListBox)
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
void CMyList::OnMouseMove(UINT nFlags, CPoint point){
BOOL out;
UINT idx = ItemFromPoint(point,out);
if(!out)
{
SetCurSel(idx);
}
else
{
SetCurSel(-1);
}
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE|TME_HOVER;
tme.dwHoverTime = 1;
TrackMouseEvent(&tme);
}
LRESULT CMyList::OnMouseLeave(WPARAM wparam, LPARAM lparam)
{
SetCurSel(-1);
return true;
}
void CMyList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData;
ASSERT(lpszText != NULL);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
// Save these value to restore them when done drawing.
COLORREF crOldTextColor = dc.GetTextColor();
COLORREF crOldBkColor = dc.GetBkColor();
// If this item is selected, set the background color
// and the text color to appropriate values. Also, erase
// rect by filling it with the background color.
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
dc.FillSolidRect(&lpDrawItemStruct->rcItem,
RGB(213,226,255));
}
else
dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);
// Draw the text.
CString slpszText = lpszText;
dc.DrawText(
slpszText,
slpszText.GetLength(),
&lpDrawItemStruct->rcItem,
DT_CENTER|DT_SINGLELINE|DT_VCENTER);
// Reset the background color and the text color back to their
// original values.
dc.SetTextColor(crOldTextColor);
dc.SetBkColor(crOldBkColor);
dc.Detach();
}
-
February 13th, 2011, 05:47 PM
#5
Re: color list of ccombo box
Never mind - i find a way to do onMouseMove and highlight items in the list :
Code:
BEGIN_MESSAGE_MAP(CMyList, CListBox)
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
void CMyList::OnMouseMove(UINT nFlags, CPoint point)
{
BOOL out;
UINT idx = ItemFromPoint(point,out);
if(!out)
{
SetCurSel(idx);
}
else
{
SetCurSel(-1);
}
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE|TME_HOVER;
tme.dwHoverTime = 1;
TrackMouseEvent(&tme);
}
LRESULT CMyList::OnMouseLeave(WPARAM wparam, LPARAM lparam)
{
SetCurSel(-1);
return true;
}
void CMyList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData;
ASSERT(lpszText != NULL);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
// Save these value to restore them when done drawing.
COLORREF crOldTextColor = dc.GetTextColor();
COLORREF crOldBkColor = dc.GetBkColor();
// If this item is selected, set the background color
// and the text color to appropriate values. Also, erase
// rect by filling it with the background color.
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
dc.FillSolidRect(&lpDrawItemStruct->rcItem,
RGB(213,226,255));
}
else
dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);
// Draw the text.
CString slpszText = lpszText;
dc.DrawText(
slpszText,
slpszText.GetLength(),
&lpDrawItemStruct->rcItem,
DT_CENTER|DT_SINGLELINE|DT_VCENTER);
// Reset the background color and the text color back to their
// original values.
dc.SetTextColor(crOldTextColor);
dc.SetBkColor(crOldBkColor);
dc.Detach();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|