Click here to Skip to main content
15,939,853 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Change background color of CComboBox Pin
Code-o-mat23-Oct-10 8:37
Code-o-mat23-Oct-10 8:37 
GeneralRe: Change background color of CComboBox Pin
mesajflaviu23-Oct-10 9:19
mesajflaviu23-Oct-10 9:19 
GeneralRe: Change background color of CComboBox Pin
Code-o-mat23-Oct-10 9:24
Code-o-mat23-Oct-10 9:24 
GeneralRe: Change background color of CComboBox Pin
mesajflaviu25-Oct-10 0:52
mesajflaviu25-Oct-10 0:52 
GeneralRe: Change background color of CComboBox Pin
Code-o-mat25-Oct-10 1:13
Code-o-mat25-Oct-10 1:13 
AnswerRe: Change background color of CComboBox Pin
Dennis Dykstra23-Oct-10 11:48
Dennis Dykstra23-Oct-10 11:48 
GeneralRe: Change background color of CComboBox Pin
mesajflaviu25-Oct-10 0:51
mesajflaviu25-Oct-10 0:51 
GeneralRe: Change background color of CComboBox [modified] Pin
Dennis Dykstra25-Oct-10 17:01
Dennis Dykstra25-Oct-10 17:01 
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 tell me which version of Visual Studio you are using.

modified on Wednesday, October 27, 2010 1:52 PM

GeneralRe: Change background color of CComboBox Pin
mesajflaviu28-Oct-10 7:37
mesajflaviu28-Oct-10 7:37 
QuestionRegarding Addition of Items in Tree Control by Type. Pin
janaswamy uday23-Oct-10 6:48
janaswamy uday23-Oct-10 6:48 
AnswerRe: Regarding Addition of Items in Tree Control by Type. Pin
Code-o-mat23-Oct-10 7:28
Code-o-mat23-Oct-10 7:28 
GeneralRe: Regarding Addition of Items in Tree Control by Type. Pin
janaswamy uday23-Oct-10 8:54
janaswamy uday23-Oct-10 8:54 
GeneralRe: Regarding Addition of Items in Tree Control by Type. Pin
Chris Losinger23-Oct-10 18:56
professionalChris Losinger23-Oct-10 18:56 
GeneralRe: Regarding Addition of Items in Tree Control by Type. Pin
Richard MacCutchan23-Oct-10 21:40
mveRichard MacCutchan23-Oct-10 21:40 
GeneralRe: Regarding Addition of Items in Tree Control by Type. Pin
janaswamy uday24-Oct-10 5:07
janaswamy uday24-Oct-10 5:07 
QuestionMenu Enabling Pin
Anil Kumar.Arvapalli23-Oct-10 2:53
Anil Kumar.Arvapalli23-Oct-10 2:53 
AnswerRe: Menu Enabling Pin
Maximilien23-Oct-10 3:27
Maximilien23-Oct-10 3:27 
GeneralRe: Menu Enabling Pin
Anil Kumar.Arvapalli23-Oct-10 3:44
Anil Kumar.Arvapalli23-Oct-10 3:44 
GeneralRe: Menu Enabling Pin
Maximilien23-Oct-10 6:02
Maximilien23-Oct-10 6:02 
GeneralRe: Menu Enabling Pin
Anil Kumar.Arvapalli23-Oct-10 21:33
Anil Kumar.Arvapalli23-Oct-10 21:33 
GeneralRe: Menu Enabling Pin
Maximilien24-Oct-10 4:25
Maximilien24-Oct-10 4:25 
QuestionHow to get mouse location in openGL Pin
Mohamed_Khalil23-Oct-10 1:55
Mohamed_Khalil23-Oct-10 1:55 
QuestionChanging the location of a CFileDialog. Pin
rentzk22-Oct-10 15:45
rentzk22-Oct-10 15:45 
AnswerRe: Changing the location of a CFileDialog. Pin
rp_suman22-Oct-10 22:10
rp_suman22-Oct-10 22:10 
AnswerRe: Changing the location of a CFileDialog. Pin
Richard MacCutchan22-Oct-10 22:47
mveRichard MacCutchan22-Oct-10 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.