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. Change background color of CComboBox [modified]

Change background color of CComboBox [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 3 Posters 3 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.
  • M Offline
    M Offline
    mesajflaviu
    wrote on last edited by
    #1

    How can I change the background color of my CComboBox , but only in the edit area , not dropdown list ? I have something like :

    class CStatusCombo : public CComboBox

    and here :

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

    // TODO: Change any attributes of the DC here
    
    // TODO: Return a different brush if the default is not desired
    
    switch(nCtlColor)
    {
    case CTLCOLOR\_LISTBOX:
    	pDC->SetTextColor(RGB(0, 255, 0));
    	pDC->SetBkColor(RGB(120,120,120));
    	return (HBRUSH)(m\_pEditBkBrush->GetSafeHandle());
    }
    
    return hbr;
    

    }

    I change background of the list area , not the edit area of the combo ... I would like to change the color of edit area just like the statusbar color ... it could be soemthing like that ?

    modified on Saturday, October 23, 2010 2:03 PM

    C D 2 Replies Last reply
    0
    • M mesajflaviu

      How can I change the background color of my CComboBox , but only in the edit area , not dropdown list ? I have something like :

      class CStatusCombo : public CComboBox

      and here :

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

      // TODO: Change any attributes of the DC here
      
      // TODO: Return a different brush if the default is not desired
      
      switch(nCtlColor)
      {
      case CTLCOLOR\_LISTBOX:
      	pDC->SetTextColor(RGB(0, 255, 0));
      	pDC->SetBkColor(RGB(120,120,120));
      	return (HBRUSH)(m\_pEditBkBrush->GetSafeHandle());
      }
      
      return hbr;
      

      }

      I change background of the list area , not the edit area of the combo ... I would like to change the color of edit area just like the statusbar color ... it could be soemthing like that ?

      modified on Saturday, October 23, 2010 2:03 PM

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Doesn't CTLCOLOR_EDIT work?

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

      M 1 Reply Last reply
      0
      • C Code o mat

        Doesn't CTLCOLOR_EDIT work?

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

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

        No , I try but didn't work ...

        C 1 Reply Last reply
        0
        • M mesajflaviu

          No , I try but didn't work ...

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          How about CTLCOLOR_STATIC?

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

          M 1 Reply Last reply
          0
          • M mesajflaviu

            How can I change the background color of my CComboBox , but only in the edit area , not dropdown list ? I have something like :

            class CStatusCombo : public CComboBox

            and here :

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

            // TODO: Change any attributes of the DC here
            
            // TODO: Return a different brush if the default is not desired
            
            switch(nCtlColor)
            {
            case CTLCOLOR\_LISTBOX:
            	pDC->SetTextColor(RGB(0, 255, 0));
            	pDC->SetBkColor(RGB(120,120,120));
            	return (HBRUSH)(m\_pEditBkBrush->GetSafeHandle());
            }
            
            return hbr;
            

            }

            I change background of the list area , not the edit area of the combo ... I would like to change the color of edit area just like the statusbar color ... it could be soemthing like that ?

            modified on Saturday, October 23, 2010 2:03 PM

            D Offline
            D Offline
            Dennis Dykstra
            wrote on last edited by
            #5

            You have to subclass the edit control of the combo box, since a combo box is a combined control. Here is an article[^] from Microsoft support describing how to do so.

            M 1 Reply Last reply
            0
            • D Dennis Dykstra

              You have to subclass the edit control of the combo box, since a combo box is a combined control. Here is an article[^] from Microsoft support describing how to do so.

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

              I do what they said to , I put the change background code , but in vain ... still don't function ...:confused:

              D 1 Reply Last reply
              0
              • C Code o mat

                How about CTLCOLOR_STATIC?

                > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

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

                It was go with CTLCOLOR_EDIT , but only when I type letters into combo control , when not the background color remain white ...

                C 1 Reply Last reply
                0
                • M mesajflaviu

                  It was go with CTLCOLOR_EDIT , but only when I type letters into combo control , when not the background color remain white ...

                  C Offline
                  C Offline
                  Code o mat
                  wrote on last edited by
                  #8

                  I just realized that you are hancling the control-color message in the combo box itself, not in its parent. Did you try with ON_WM_CTLCOLOR_REFELECT or ON_WM_CTLCOLOR?

                  > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

                  1 Reply Last reply
                  0
                  • M mesajflaviu

                    I do what they said to , I put the change background code , but in vain ... still don't function ...:confused:

                    D Offline
                    D Offline
                    Dennis Dykstra
                    wrote on last edited by
                    #9

                    I've just tried it with an MFC dialog application. It's not perfect because the color change doesn't "stick" when the edit control has the focus but it comes back when you tab away from the combo box. You'd need to do some more work to make it work the way you want when the edit control has the focus, but this should give you a starting point. The class that I derived from CComboBox is named CColorEditCombo. In the Visual Studio designer I added a CComboBox control to the dialog window. With the ClassWizard I added a CComboBox control variable named m_ctlComboColorEdit. Then in the header file for the dialog class I changed the declaration for this variable to the following:

                    CColorEditCombo m_ctlComboColorEdit;

                    This associates that control with the new CColorEditCombo class rather than the parent CComboBox class. In the class file for CColorEditCombo I added a handler for the WM_CTLCOLOR message as follows:

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

                    switch (nCtlColor)
                    {
                    case CTLCOLOR\_EDIT:
                    case CTLCOLOR\_MSGBOX:
                        // Subclass the edit control.
                        if (m\_edit.GetSafeHwnd() == NULL)
                            {
                            m\_edit.SubclassWindow(pWnd->GetSafeHwnd());
                            }
                        // Set the background color for the edit control.
                        pDC->SetBkColor(RGB(255, 255, 0));
                        // Recolor the edit control.
                        m\_brush.DeleteObject();
                        m\_brush.CreateSolidBrush(RGB(255, 255, 0));
                        hbr = (HBRUSH) m\_brush.GetSafeHandle();
                        return hbr;
                    
                    case CTLCOLOR\_LISTBOX:
                        // Subclass the listbox control.
                        if (m\_listBox.GetSafeHwnd() == NULL)
                            {
                            m\_listBox.SubclassWindow(pWnd->GetSafeHwnd());
                            }
                        // Add recoloring actions for the listbox here if desired.
                     }
                    
                    return hbr;
                    }
                    

                    Note the member variable named m_brush. This has to be declared in the header file for the CColorEditCombo class as a CBrush object. It should be deleted in the class destructor as well as just before the CreateSolidBrush statement shown above. If you'd like, I can send you the solution for the very simple MFC dialog application I put together as a test to make sure this procedure works. You'll have to give me your email address and

                    M 1 Reply Last reply
                    0
                    • D Dennis Dykstra

                      I've just tried it with an MFC dialog application. It's not perfect because the color change doesn't "stick" when the edit control has the focus but it comes back when you tab away from the combo box. You'd need to do some more work to make it work the way you want when the edit control has the focus, but this should give you a starting point. The class that I derived from CComboBox is named CColorEditCombo. In the Visual Studio designer I added a CComboBox control to the dialog window. With the ClassWizard I added a CComboBox control variable named m_ctlComboColorEdit. Then in the header file for the dialog class I changed the declaration for this variable to the following:

                      CColorEditCombo m_ctlComboColorEdit;

                      This associates that control with the new CColorEditCombo class rather than the parent CComboBox class. In the class file for CColorEditCombo I added a handler for the WM_CTLCOLOR message as follows:

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

                      switch (nCtlColor)
                      {
                      case CTLCOLOR\_EDIT:
                      case CTLCOLOR\_MSGBOX:
                          // Subclass the edit control.
                          if (m\_edit.GetSafeHwnd() == NULL)
                              {
                              m\_edit.SubclassWindow(pWnd->GetSafeHwnd());
                              }
                          // Set the background color for the edit control.
                          pDC->SetBkColor(RGB(255, 255, 0));
                          // Recolor the edit control.
                          m\_brush.DeleteObject();
                          m\_brush.CreateSolidBrush(RGB(255, 255, 0));
                          hbr = (HBRUSH) m\_brush.GetSafeHandle();
                          return hbr;
                      
                      case CTLCOLOR\_LISTBOX:
                          // Subclass the listbox control.
                          if (m\_listBox.GetSafeHwnd() == NULL)
                              {
                              m\_listBox.SubclassWindow(pWnd->GetSafeHwnd());
                              }
                          // Add recoloring actions for the listbox here if desired.
                       }
                      
                      return hbr;
                      }
                      

                      Note the member variable named m_brush. This has to be declared in the header file for the CColorEditCombo class as a CBrush object. It should be deleted in the class destructor as well as just before the CreateSolidBrush statement shown above. If you'd like, I can send you the solution for the very simple MFC dialog application I put together as a test to make sure this procedure works. You'll have to give me your email address and

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

                      I want to thank you for your solution , I will try it , and let you know if I did it ! I use VC6 .

                      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