CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2009
    Posts
    399

    Change text and bkg color of CComboBox

    I have a derived CComboBox class, where I tried to change text and background of edit from CComboBox, just like this:
    Code:
    HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    	HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
    
    	// TODO: Change any attributes of the DC here
    
    //	if(CTLCOLOR_EDIT == nCtlColor || CTLCOLOR_MSGBOX == nCtlColor)
    	{
    		pDC->SetTextColor(RGB(255, 255, 0));
    		pDC->SetBkColor(RGB(255, 0, 0));
    		pDC->SetBkMode(TRANSPARENT);
    		hbr = m_hBrush;
    	}
    
    	// TODO: Return a different brush if the default is not desired
    	return hbr;
    }
    but is not working if CComboBox has CBS_DROPDOWNLIST style ... is working only for CBS_DROPDOWN style ... why ?
    I attached a test app ...

  2. #2
    Join Date
    Jan 2009
    Posts
    399

    Re: Change text and bkg color of CComboBox

    I attach the app when I can ... I don't know why upload file manager didn't working ...

  3. #3
    Join Date
    Jan 2009
    Posts
    399

    Re: Change text and bkg color of CComboBox

    I uploaded the test project here:
    http://www.filedropper.com/testcolorcombo

    Here I attached the app without style, where color of text and background is custom colored ...
    Attached Files Attached Files
    Last edited by mesajflaviu; December 5th, 2014 at 02:49 AM.

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

    Re: Change text and bkg color of CComboBox

    Quote Originally Posted by mesajflaviu View Post
    I have a derived CComboBox class, where I tried to change text and background of edit from CComboBox, just like this:
    Code:
    HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    	HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
    
    	// TODO: Change any attributes of the DC here
    
    //	if(CTLCOLOR_EDIT == nCtlColor || CTLCOLOR_MSGBOX == nCtlColor)
    	{
    		pDC->SetTextColor(RGB(255, 255, 0));
    		pDC->SetBkColor(RGB(255, 0, 0));
    		pDC->SetBkMode(TRANSPARENT);
    		hbr = m_hBrush;
    	}
    
    	// TODO: Return a different brush if the default is not desired
    	return hbr;
    }
    but is not working if CComboBox has CBS_DROPDOWNLIST style ... is working only for CBS_DROPDOWN style ... why ?
    I attached a test app ...
    It is not working because CBS_DROPDOWNLIST combo does NOT have any edit control.
    This is the quotation from MSDN from October 2000 (sorry, MSDN is currently unavailable):
    HOWTO: Change the Color of an MFC Child Control Class

    --------------------------------------------------------------------------------
    The information in this article applies to:

    The Microsoft Foundation Classes (MFC), used with:
    Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51, 1.52
    Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 2.1, 2.2, 4.0, 5.0, 6.0

    --------------------------------------------------------------------------------


    SUMMARY
    To change the color scheme of a standard class of controls in an MFC-based application, follow these steps:



    Derive a class from the standard control class, such as CEdit.


    Define a static member variable of class CBrush to be the brush for that class of controls.


    Override the control's member function OnChildNotify(), handle the message WM_CTLCOLOR, and use the new brush. In MFC 4.0, this could also be done by using an ON_WM_CTLCOLOR_REFLECT handler.


    NOTE: In 32-bit Windows, the controls do not send the WM_CTLCOLOR message. They send WM_CTLCOLORxxx messages, where xxx is the type of control. For example, static control sends the WM_CTLCOLORSTATIC message.



    MORE INFORMATION
    This approach works for list boxes, the list boxes of combo boxes, button controls, edit controls, static controls, message boxes, and dialog boxes. (This approach does not work for push buttons and the CRichEditCtrl. The color of a standard CButton object is determined by system settings. If you want a different color for push buttons, use a CBitmapButton. To change the color of a CRichEditCtrl use its member functions.)

    For an alternative approach, please see the following article in the Microsoft Knowledge Base:

    Q117778 Changing the Background Color of an MFC Edit Control
    It illustrates how to change the background color of a control in the parent window.

    When a control is about to be redrawn, it sends the message WM_CTLCOLOR to its parent. This message is handled by the OnCtlColor() member function of the parent.

    OnCtlColor() allows the parent to modify the drawing of the child by:


    Specifying the background brush.


    Changing the text color.


    Making other changes to the device context with which the drawing is to be done.


    One of the first things the default implementation of OnCtlColor() does is to call the OnChildNotify() member function of the child that sent the message. By overriding this OnChildNotify() member function, the child can determine its own color scheme, instead of taking it from the parent.

    The following sample code defines a class of edit controls with red text on a green background. The sample code shows only what is necessary to change the color of the controls. It does not include code generated by the ClassWizard.

    Sample Code

    Code:
       /* Compile options needed:  Default
       */ 
    
       // NOTE:  The sample code is for 32-bit. It has to be modified for
       // 16-bit. See the comment below.
    
       // ** MYEDIT.H **
    
       class CMyEdit : public CEdit
       {
       public:
          BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM Param,
              LRESULT* pLResult);
       protected:
          static CBrush m_brush;
       };
    
       // ** MYEDIT.CPP **
    
       #include "myedit.h"
    
       // Create a green brush for the background for the class of controls:
       CBrush CMyEdit::m_brush(RGB(0,128,0));
    
       BOOL CMyEdit::OnChildNotify(UINT message, WPARAM wParam,
                                   LPARAM lParam, LRESULT* pLResult)
       {
       // If "message" is not the message you're after, do default processing:
    
       // For 16-bit applications change WM_CTLCOLOREDIT to WM_CTLCOLOR
          if (message != WM_CTLCOLOREDIT)
          {
             return CEdit::OnChildNotify(message,wParam,lParam,pLResult);
          }
    
       // Set the text foreground to red:
          HDC hdcChild = (HDC)wParam;
          SetTextColor(hdcChild, RGB(0,0,255));
    
       // Set the text background to green:
          SetBkColor(hdcChild, RGB(0,128,0));
    
       // Send what would have been the return value of OnCtlColor() - the
       // brush handle - back in pLResult:
          *pLResult = (LRESULT)(m_brush.GetSafeHandle());
    
       // Return TRUE to indicate that the message was handled:
          return TRUE;
       }
    Victor Nijegorodov

  5. #5
    Join Date
    Jan 2009
    Posts
    399

    Re: Change text and bkg color of CComboBox

    Yes, is working now
    Here is the code:
    Code:
    CMyComboBox::CMyComboBox()
    {
    	m_Brush.CreateSolidBrush(RGB(255, 0, 0));
    }
    
    CMyComboBox::~CMyComboBox()
    {
    	m_Brush.DeleteObject();
    }
    
    
    BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)
    	//{{AFX_MSG_MAP(CMyComboBox)
    		// NOTE - the ClassWizard will add and remove mapping macros here.
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CMyComboBox message handlers
    
    BOOL CMyComboBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    
    	if(WM_CTLCOLOREDIT != message)
    		return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);
    
    	HDC hdcChild = (HDC)wParam;
    	if(NULL != hdcChild)
    	{
    		SetBkMode(hdcChild, TRANSPARENT);
    		SetTextColor(hdcChild, RGB(255, 255, 0));
    		SetBkColor(hdcChild, RGB(255, 0, 0));
    		*pLResult = (LRESULT)(m_Brush.GetSafeHandle());
    	}
    
    	return TRUE;
    //	return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);
    }
    I still have a question: yes, the text and bkg color is changed now when combobox has CBS_DROPDOWNLIST style, but this solution doesn't change text and bkg color when combobox has CBS_DROPDOWN style ...

    so, when combobox has CBS_DROPDOWN style, I should change text and bkg color through OnCtlColor handler, and when combobox has CBS_DROPDOWNLIST style I should override OnChildNotify ? Am I right ?
    Last edited by mesajflaviu; November 27th, 2014 at 04:35 AM.

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

    Re: Change text and bkg color of CComboBox

    Quote Originally Posted by mesajflaviu View Post
    ..., but this solution doesn't change text and bkg color when combobox has CBS_DROPDOWN style ...

    so, when combobox has CBS_DROPDOWN style, I should change text and bkg color through OnCtlColor handler, and when combobox has CBS_DROPDOWNLIST style I should override OnChildNotify ? I am right ?
    Or you could subclass the combo's edit control and change color in this subclass. See also http://computer-programming-forum.co...670aaf6e86.htm
    Victor Nijegorodov

  7. #7
    Join Date
    Jan 2009
    Posts
    399

    Re: Change text and bkg color of CComboBox

    Ok, kindly thank you !

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

    Re: Change text and bkg color of CComboBox

    You are welcome!
    Victor Nijegorodov

  9. #9
    Join Date
    Jan 2009
    Posts
    399

    Re: Change text and bkg color of CComboBox

    There was remain a small issue: this solution doesn't work if the control has style ... if doesn't have any style, everything is allright ... where should I search to solve this issue ?
    This is the manifest code:
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1"
                     manifestVersion="1.0">
    <assemblyIdentity
        version="1.0.0.0"
        processorArchitecture="X86"
        name="Microsoft.Windows.Sinis.Software"
        type="win32"
    />
    <description>sinis software</description>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity
                type="win32"
                name="Microsoft.Windows.Common-Controls"
                version="6.0.0.0"
                processorArchitecture="X86"
                publicKeyToken="6595b64144ccf1df"
                language="*"
            />
        </dependentAssembly>
    </dependency>
    </assembly>
    inside of this manifest could be the problem ?

  10. #10
    Join Date
    Jan 2009
    Posts
    399

    Re: Change text and bkg color of CComboBox

    I succeded to upload a test app that illustrate the issue ... here is the code, that is working well if the app hasn't style:
    Code:
    BOOL CMyComboBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    
    	if(WM_CTLCOLOREDIT != message)
    		return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);
    
    	HDC hdcChild = (HDC)wParam;
    	if(NULL != hdcChild)
    	{
    		SetBkMode(hdcChild, TRANSPARENT);
    		SetTextColor(hdcChild, RGB(255, 255, 0));
    		SetBkColor(hdcChild, RGB(255, 0, 0));
    		*pLResult = (LRESULT)(m_Brush.GetSafeHandle());
    	}
    
    	return TRUE;
    //	return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);
    }
    soon as I setup a style on my app, the background and the text color is not custom colored ... why ?
    Attached Files Attached Files

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

    Re: Change text and bkg color of CComboBox

    If you want to customize the look of controls which have Visual Styles then probably you have to use Visual Styles API.
    Last edited by ovidiucucu; December 7th, 2014 at 07:37 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

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