Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can I change background/text color of CComboBox ?

How can I change background/text color of CComboBox ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
17 Posts 5 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _Flaviu

    I mean drop down list , not edit box where I succeded :

    HBRUSH CComboBoxExt::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    // HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here
    
    if(nCtlColor == CTLCOLOR\_EDIT)
    {
    	if(! m\_bAlertText)pDC->SetTextColor(COLOR\_BLACK);
    	else pDC->SetTextColor(COLOR\_RED);
    	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
    	else pDC->SetBkColor(COLOR\_ALERT);
    }
    
    pDC->SetBkMode(TRANSPARENT);
    
    // TODO: Return a different brush if the default is not desired
    
    if(m\_bAlertBkg)return m\_hBrushAlert;
    
    return m\_hBrushWhite;
    

    }

    well , I change CTLCOLOR_EDIT with CTLCOLOR_LISTBOX but with no effect ... I don't know why ...

    O Offline
    O Offline
    Ozer Karaagac
    wrote on last edited by
    #8

    In above code, that is, subclassed combo box's OnCtlColor() member, you should handle CTLCOLOR_LISTBOX for dropped-down list box. May be, subclassing fails. To ensure that above code is called, put the following line on top of function. TRACE(_T("CComboBoxExt::OnCtlColor() was called with nCtlColor: %d\n"), nCtlColor); And watch output window to see above phrase.

    _ 2 Replies Last reply
    0
    • _ _Flaviu

      I mean drop down list , not edit box where I succeded :

      HBRUSH CComboBoxExt::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      {
      // HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

      // TODO: Change any attributes of the DC here
      
      if(nCtlColor == CTLCOLOR\_EDIT)
      {
      	if(! m\_bAlertText)pDC->SetTextColor(COLOR\_BLACK);
      	else pDC->SetTextColor(COLOR\_RED);
      	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
      	else pDC->SetBkColor(COLOR\_ALERT);
      }
      
      pDC->SetBkMode(TRANSPARENT);
      
      // TODO: Return a different brush if the default is not desired
      
      if(m\_bAlertBkg)return m\_hBrushAlert;
      
      return m\_hBrushWhite;
      

      }

      well , I change CTLCOLOR_EDIT with CTLCOLOR_LISTBOX but with no effect ... I don't know why ...

      V Offline
      V Offline
      venkatmakam
      wrote on last edited by
      #9

      I did not tried the subclassed combobox, but according to msdn, http://msdn.microsoft.com/en-us/library/0wwk06hc(v=vs.80).aspx[^] In subclassed combobox you have to handle CTLCOLOR_LISTBOX to change the color. I tried handling OnCtlColor in dialog box,with nCtrClr == (IDOFCOMBOBOX),it is working

      HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      {
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

      if (pWnd->GetDlgCtrlID() == IDC_COMBO1)
      {
      pDC->SetBkMode(TRANSPARENT);
      pDC->SetTextColor(RGB(128,128, 128));
      hbr = m_brush;
      }

      return hbr;
      }

      _ 1 Reply Last reply
      0
      • O Ozer Karaagac

        In above code, that is, subclassed combo box's OnCtlColor() member, you should handle CTLCOLOR_LISTBOX for dropped-down list box. May be, subclassing fails. To ensure that above code is called, put the following line on top of function. TRACE(_T("CComboBoxExt::OnCtlColor() was called with nCtlColor: %d\n"), nCtlColor); And watch output window to see above phrase.

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #10

        I think I am in dead point ... I use already OnCtlColorListBox but for other reason , and with other signature :

        ON_MESSAGE(WM_CTLCOLORLISTBOX, OnCtlColorListBox)
        ...
        ...

        LRESULT CComboBoxExt::OnCtlColorListBox(WPARAM wParam, LPARAM lParam)
        {
        // If the listbox hasn't been subclassed yet, do so...
        if(m_hListBox == NULL)
        {
        HWND hWnd = reinterpret_cast(lParam);
        if(hWnd != 0 && hWnd != m_hWnd)
        {
        // Save the listbox handle
        m_hListBox = hWnd;
        // Do the subclassing
        m_pWndProc = reinterpret_cast(GetWindowLong(m_hListBox, GWL_WNDPROC));
        SetWindowLong(m_hListBox, GWL_WNDPROC, reinterpret_cast(ComboBoxListBoxProc));
        }
        }

        return 1;
        

        }

        to get a handle to list box window ...

        O 1 Reply Last reply
        0
        • O Ozer Karaagac

          In above code, that is, subclassed combo box's OnCtlColor() member, you should handle CTLCOLOR_LISTBOX for dropped-down list box. May be, subclassing fails. To ensure that above code is called, put the following line on top of function. TRACE(_T("CComboBoxExt::OnCtlColor() was called with nCtlColor: %d\n"), nCtlColor); And watch output window to see above phrase.

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #11

          To make a review :

          ON\_MESSAGE(WM\_CTLCOLORLISTBOX, OnCtlColorListBox)
          ON\_WM\_CTLCOLOR()
          

          LRESULT CComboBoxExt::OnCtlColorListBox(WPARAM wParam, LPARAM lParam)
          {
          // If the listbox hasn't been subclassed yet, do so...
          if(m_hListBox == NULL)
          {
          HWND hWnd = reinterpret_cast(lParam);
          if(hWnd != 0 && hWnd != m_hWnd)
          {
          // Save the listbox handle
          m_hListBox = hWnd;
          // Do the subclassing
          m_pWndProc = reinterpret_cast(GetWindowLong(m_hListBox, GWL_WNDPROC));
          SetWindowLong(m_hListBox, GWL_WNDPROC, reinterpret_cast(ComboBoxListBoxProc));
          }
          }

          return 1;
          

          }

          HBRUSH CComboBoxExt::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
          {
          HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

          // TODO: Change any attributes of the DC here
          
          if(nCtlColor == CTLCOLOR\_EDIT)
          {
          	if(! m\_bAlertText)pDC->SetTextColor(COLOR\_BLACK);
          	else pDC->SetTextColor(COLOR\_RED);
          	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
          	else
          	{
          		pDC->SetBkColor(COLOR\_ALERT);
          		hbr = m\_hBrushAlert;
          	}
          }
          
          pDC->SetBkMode(TRANSPARENT);
          
          // TODO: Return a different brush if the default is not desired
          
          return hbr;
          

          }

          O 2 Replies Last reply
          0
          • _ _Flaviu

            I think I am in dead point ... I use already OnCtlColorListBox but for other reason , and with other signature :

            ON_MESSAGE(WM_CTLCOLORLISTBOX, OnCtlColorListBox)
            ...
            ...

            LRESULT CComboBoxExt::OnCtlColorListBox(WPARAM wParam, LPARAM lParam)
            {
            // If the listbox hasn't been subclassed yet, do so...
            if(m_hListBox == NULL)
            {
            HWND hWnd = reinterpret_cast(lParam);
            if(hWnd != 0 && hWnd != m_hWnd)
            {
            // Save the listbox handle
            m_hListBox = hWnd;
            // Do the subclassing
            m_pWndProc = reinterpret_cast(GetWindowLong(m_hListBox, GWL_WNDPROC));
            SetWindowLong(m_hListBox, GWL_WNDPROC, reinterpret_cast(ComboBoxListBoxProc));
            }
            }

            return 1;
            

            }

            to get a handle to list box window ...

            O Offline
            O Offline
            Ozer Karaagac
            wrote on last edited by
            #12

            Yes. This blocks OnCtlColor(CTLCOLOR_LISTBOX). OnCtlColor() (WM_CTLCOLOR) is retained from 16Bit Windows and should be managed by MFC using newer messages like WM_CTLCOLORLISTBOX. You can also use this handler to do your work, the HDC is wParam and you need to return a valid HBRUSH. But, above return value is invalid (as brush). Probably, that listbox should paint itself if this doesn't produce a problem. In this case, how could you paint again?

            1 Reply Last reply
            0
            • _ _Flaviu

              To make a review :

              ON\_MESSAGE(WM\_CTLCOLORLISTBOX, OnCtlColorListBox)
              ON\_WM\_CTLCOLOR()
              

              LRESULT CComboBoxExt::OnCtlColorListBox(WPARAM wParam, LPARAM lParam)
              {
              // If the listbox hasn't been subclassed yet, do so...
              if(m_hListBox == NULL)
              {
              HWND hWnd = reinterpret_cast(lParam);
              if(hWnd != 0 && hWnd != m_hWnd)
              {
              // Save the listbox handle
              m_hListBox = hWnd;
              // Do the subclassing
              m_pWndProc = reinterpret_cast(GetWindowLong(m_hListBox, GWL_WNDPROC));
              SetWindowLong(m_hListBox, GWL_WNDPROC, reinterpret_cast(ComboBoxListBoxProc));
              }
              }

              return 1;
              

              }

              HBRUSH CComboBoxExt::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
              {
              HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

              // TODO: Change any attributes of the DC here
              
              if(nCtlColor == CTLCOLOR\_EDIT)
              {
              	if(! m\_bAlertText)pDC->SetTextColor(COLOR\_BLACK);
              	else pDC->SetTextColor(COLOR\_RED);
              	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
              	else
              	{
              		pDC->SetBkColor(COLOR\_ALERT);
              		hbr = m\_hBrushAlert;
              	}
              }
              
              pDC->SetBkMode(TRANSPARENT);
              
              // TODO: Return a different brush if the default is not desired
              
              return hbr;
              

              }

              O Offline
              O Offline
              Ozer Karaagac
              wrote on last edited by
              #13

              Don't use both. Use 1st one.

              LRESULT CComboBoxExt::OnCtlColorListBox(WPARAM wParam, LPARAM lParam)
              {
              // If the listbox hasn't been subclassed yet, do so
              ...

              // CWnd \*pWnd = CWnd::FromHandle((HWND)lParam);
              // UINT nID = npWnd->GetDlgCtrlID();
              // nCtlColor = CTLCOLOR\_LISTBOX;
              
              CDC\* pDC = CDC::FromHandle((HDC)wParam);
              
              // move your attributes here
              ...
              
              return hbr;
              

              }

              1 Reply Last reply
              0
              • _ _Flaviu

                To make a review :

                ON\_MESSAGE(WM\_CTLCOLORLISTBOX, OnCtlColorListBox)
                ON\_WM\_CTLCOLOR()
                

                LRESULT CComboBoxExt::OnCtlColorListBox(WPARAM wParam, LPARAM lParam)
                {
                // If the listbox hasn't been subclassed yet, do so...
                if(m_hListBox == NULL)
                {
                HWND hWnd = reinterpret_cast(lParam);
                if(hWnd != 0 && hWnd != m_hWnd)
                {
                // Save the listbox handle
                m_hListBox = hWnd;
                // Do the subclassing
                m_pWndProc = reinterpret_cast(GetWindowLong(m_hListBox, GWL_WNDPROC));
                SetWindowLong(m_hListBox, GWL_WNDPROC, reinterpret_cast(ComboBoxListBoxProc));
                }
                }

                return 1;
                

                }

                HBRUSH CComboBoxExt::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
                {
                HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

                // TODO: Change any attributes of the DC here
                
                if(nCtlColor == CTLCOLOR\_EDIT)
                {
                	if(! m\_bAlertText)pDC->SetTextColor(COLOR\_BLACK);
                	else pDC->SetTextColor(COLOR\_RED);
                	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
                	else
                	{
                		pDC->SetBkColor(COLOR\_ALERT);
                		hbr = m\_hBrushAlert;
                	}
                }
                
                pDC->SetBkMode(TRANSPARENT);
                
                // TODO: Return a different brush if the default is not desired
                
                return hbr;
                

                }

                O Offline
                O Offline
                Ozer Karaagac
                wrote on last edited by
                #14

                BTW, beware that list box is already subclassed by other reason. If it paints itself without using windows behaviour, that is, without sending WM_CTLCOLOR notifications to its parent, your attempt can be failed again.

                1 Reply Last reply
                0
                • V venkatmakam

                  I did not tried the subclassed combobox, but according to msdn, http://msdn.microsoft.com/en-us/library/0wwk06hc(v=vs.80).aspx[^] In subclassed combobox you have to handle CTLCOLOR_LISTBOX to change the color. I tried handling OnCtlColor in dialog box,with nCtrClr == (IDOFCOMBOBOX),it is working

                  HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
                  {
                  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

                  if (pWnd->GetDlgCtrlID() == IDC_COMBO1)
                  {
                  pDC->SetBkMode(TRANSPARENT);
                  pDC->SetTextColor(RGB(128,128, 128));
                  hbr = m_brush;
                  }

                  return hbr;
                  }

                  _ Offline
                  _ Offline
                  _Flaviu
                  wrote on last edited by
                  #15

                  I solve the problem in the follow matter :

                  // header
                  CListBox m_ListBox;

                  // implementation
                  ON_WM_CTLCOLOR()

                  HBRUSH CComboBoxExt::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
                  {
                  HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

                  // TODO: Change any attributes of the DC here
                  
                  if(nCtlColor == CTLCOLOR\_EDIT)
                  {
                  	if(m\_Edit.GetSafeHwnd() == NULL)
                  	{
                  		m\_Edit.SubclassWindow(pWnd->GetSafeHwnd());
                  	}
                  	if(! m\_bAlertText)pDC->SetTextColor(COLOR\_BLACK);
                  	else pDC->SetTextColor(COLOR\_RED);
                  	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
                  	else
                  	{
                  		pDC->SetBkColor(COLOR\_ALERT);
                  		hbr = m\_hBrushAlert;
                  	}
                  }
                  
                  if(nCtlColor == CTLCOLOR\_LISTBOX)
                  {
                  	if(m\_ListBox.GetSafeHwnd() == NULL)
                  	{
                  		m\_ListBox.SubclassWindow(pWnd->GetSafeHwnd());
                  		m\_pWndProc = reinterpret\_cast<WNDPROC>(GetWindowLong(m\_ListBox, GWL\_WNDPROC));
                  		SetWindowLong(m\_ListBox, GWL\_WNDPROC, reinterpret\_cast<long>(ComboBoxListBoxProc));
                  	}
                  	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
                  	else
                  	{
                  		pDC->SetBkColor(COLOR\_ALERT);
                  		hbr = m\_hBrushAlert;
                  	}
                  }
                  
                  pDC->SetBkMode(TRANSPARENT);
                  
                  // TODO: Return a different brush if the default is not desired
                  
                  return hbr;
                  

                  }

                  I get handle of drop down list box in OnCtlColor but I still have an question : How can I change text / background color of edit while combo box have CBS_DROPDOWNLIST style ?

                  _ 1 Reply Last reply
                  0
                  • _ _Flaviu

                    I solve the problem in the follow matter :

                    // header
                    CListBox m_ListBox;

                    // implementation
                    ON_WM_CTLCOLOR()

                    HBRUSH CComboBoxExt::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
                    {
                    HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

                    // TODO: Change any attributes of the DC here
                    
                    if(nCtlColor == CTLCOLOR\_EDIT)
                    {
                    	if(m\_Edit.GetSafeHwnd() == NULL)
                    	{
                    		m\_Edit.SubclassWindow(pWnd->GetSafeHwnd());
                    	}
                    	if(! m\_bAlertText)pDC->SetTextColor(COLOR\_BLACK);
                    	else pDC->SetTextColor(COLOR\_RED);
                    	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
                    	else
                    	{
                    		pDC->SetBkColor(COLOR\_ALERT);
                    		hbr = m\_hBrushAlert;
                    	}
                    }
                    
                    if(nCtlColor == CTLCOLOR\_LISTBOX)
                    {
                    	if(m\_ListBox.GetSafeHwnd() == NULL)
                    	{
                    		m\_ListBox.SubclassWindow(pWnd->GetSafeHwnd());
                    		m\_pWndProc = reinterpret\_cast<WNDPROC>(GetWindowLong(m\_ListBox, GWL\_WNDPROC));
                    		SetWindowLong(m\_ListBox, GWL\_WNDPROC, reinterpret\_cast<long>(ComboBoxListBoxProc));
                    	}
                    	if(! m\_bAlertBkg)pDC->SetBkColor(COLOR\_WHITE);
                    	else
                    	{
                    		pDC->SetBkColor(COLOR\_ALERT);
                    		hbr = m\_hBrushAlert;
                    	}
                    }
                    
                    pDC->SetBkMode(TRANSPARENT);
                    
                    // TODO: Return a different brush if the default is not desired
                    
                    return hbr;
                    

                    }

                    I get handle of drop down list box in OnCtlColor but I still have an question : How can I change text / background color of edit while combo box have CBS_DROPDOWNLIST style ?

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #16

                    Finllay I solve my problem , and for that I thank you very much , for you kindness and for your patient . ! Best wishes !

                    M 1 Reply Last reply
                    0
                    • _ _Flaviu

                      Finllay I solve my problem , and for that I thank you very much , for you kindness and for your patient . ! Best wishes !

                      M Offline
                      M Offline
                      mesajflaviu
                      wrote on last edited by
                      #17

                      I solve the problem in follow way :

                      class CMyComboBox : public CComboBox
                      {
                      // ...
                      afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
                      // ...
                      };

                      and

                      //...
                      ON_WM_CTLCOLOR_REFLECT()
                      //}}AFX_MSG_MAP
                      END_MESSAGE_MAP()

                      /////////////////////////////////////////////////////////////////////////////
                      // CMyComboBox message handlers

                      HBRUSH CMyComboBox::CtlColor(CDC* pDC, UINT nCtlColor)
                      {
                      HBRUSH hBrush = NULL;
                      switch(nCtlColor)
                      {
                      case CTLCOLOR_EDIT:
                      {
                      pDC->SetBkColor(RGB(255, 0, 0));
                      pDC->SetTextColor(RGB(255, 255, 255));
                      hBrush = m_brush;
                      }
                      break;
                      //...
                      }
                      return hBrush;
                      }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups