color list of ccombo box
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: color list of ccombo box

  1. #1
    Join Date
    Aug 2009
    Posts
    78

    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

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,059

    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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Aug 2009
    Posts
    78

    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 .

  4. #4
    Join Date
    Aug 2009
    Posts
    78

    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();
    }

  5. #5
    Join Date
    Aug 2009
    Posts
    78

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center