CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2002
    Posts
    756

    Change color of combo box

    Hi, ALL,
    Is there a way to change the background color of the combo box? Asking because there is no CTLCOLOR event for this control...

    Thank you.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Change color of combo box

    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2011
    Posts
    23

    Re: Change color of combo box

    You can manually add your own CTLCOLOR functionality, CWnd supports it.

    Code:
    //in the .h file
      afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
    
    //in the .cpp file
      ON_WM_CTLCOLOR()
    
    
    HBRUSH CYourClass::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
      int
        nCtrlID = pWnd->GetDlgCtrlID();
      HBRUSH
        hbr     = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
      switch(nCtlColor)
      {
        case CTLCOLOR_EDIT:
          pDC->SetTextColor(RGB(255, 0, 0));
          hbr = (HBRUSH)GetStockObject(WHITE_BRUSH);
          break;
    
        case CTLCOLOR_STATIC:
          pDC->SetTextColor(RGB(0, 0, 128));
          pDC->SetBkColor(GetSysColor(COLOR_BTNFACE));
          hbr = GetSysColorBrush(COLOR_BTNFACE);
          break;
    
        case CTLCOLOR_BTN:
          pDC->SetTextColor(RGB(128, 0, 0));
          pDC->SetBkColor(GetSysColor(COLOR_BTNFACE));
          hbr = GetSysColorBrush(COLOR_BTNFACE);
          break;
    
        case CTLCOLOR_LISTBOX:
          pDC->SetTextColor(RGB(128, 0, 0));
          pDC->SetBkColor(GetSysColor(COLOR_BTNFACE));
          hbr = GetSysColorBrush(COLOR_BTNFACE);
          break;
    
        default:
          hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
          break;
      }
    
      return hbr;
    }
    you can add any MFC functionality manually, or you can use the wizzards . . . doesn't really much matter. . . There are many other settings you can affect from here as well . . .

    Perfect Health and Clarity of Mind,

    --Victor

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Change color of combo box

    Quote Originally Posted by He_That_Is View Post
    You can manually add your own CTLCOLOR functionality, CWnd supports it.
    That doesn't mean "adding your own CTLCOLOR functionality" but mapping a handler function for WM_CTLCOLOR message.

    Quote Originally Posted by He_That_Is View Post
    you can add any MFC functionality manually, or you can use the wizzards . . . doesn't really much matter. . . There are many other settings you can affect from here as well . . .
    Of course, you can manually map any message but the wizard helps you doing it faster and prevents making mistakes.
    What "other settings"?
    Last edited by ovidiucucu; January 11th, 2012 at 03:39 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Aug 2002
    Posts
    756

    Re: Change color of combo box

    Thank you for the reply, guys.
    Is there a straight Win32 way - no MFC?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Change color of combo box

    Quote Originally Posted by OneEyeMan View Post
    Thank you for the reply, guys.
    Is there a straight Win32 way - no MFC?
    Yes. You could implement owner (or custom) drawing for your control.
    Or you could try to handle both WM_CTLCOLORLISTBOX and WM_CTLCOLOREDIT messages (sorry, I never did it myself in plain Win32, so I can only guess!(
    Last edited by VictorN; February 15th, 2012 at 08:45 AM. Reason: Some correctins from Ovidiucucu about WM_CTLCOLOR message
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured