CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2019
    Posts
    82

    Changing background Color of CBS_DROPDOWN CCombobox

    Any idea how to quickly change background color of CBS_DROPDOWNLIST CComboBox to white. since CBS_DROPDOWN has white background, i also want CBS_DROPDOWNLIST to have white background.

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

    Re: Changing background Color of CBS_DROPDOWN CCombobox

    Did you try to handle WM_CTLCOLOR?
    Also see this thread.
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2019
    Posts
    82

    Re: Changing background Color of CBS_DROPDOWN CCombobox

    I tried below code
    // MyComboBox.h
    Code:
     
    #if !defined(AFX_MYCOMBOBOX_H__6C1728B4_F098_408B_919A_A8DA393AA426__INCLUDED_)
     
    #define AFX_MYCOMBOBOX_H__6C1728B4_F098_408B_919A_A8DA393AA426__INCLUDED_
     
     
    #if _MSC_VER > 1000
     
    #pragma once
     
    #endif // _MSC_VER > 1000
     
    // MyComboBox.h : header file
     
    //
     
     
    /////////////////////////////////////////////////////////////////////////////
     
    // CMyComboBox window
     
     
    class CMyComboBox : public CComboBox
     
    {
     
    // Construction
     
    public:
     
        CMyComboBox();
     
     
    // Attributes
     
    public:
     
        CBrush m_Brush;
     
     
    // Operations
     
    public:
     
     
    // Overrides
     
        // ClassWizard generated virtual function overrides
     
        //{{AFX_VIRTUAL(CMyComboBox)
     
        public:
     
        virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult);
     
        //}}AFX_VIRTUAL
     
     
    // Implementation
     
    public:
     
        virtual ~CMyComboBox();
     
     
        // Generated message map functions
     
    protected:
     
        //{{AFX_MSG(CMyComboBox)
     
            // NOTE - the ClassWizard will add and remove member functions here.
     
        //}}AFX_MSG
     
     
        DECLARE_MESSAGE_MAP()
     
    };
     
     
    /////////////////////////////////////////////////////////////////////////////
     
     
    //{{AFX_INSERT_LOCATION}}
     
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
     
     
    #endif // !defined(AFX_MYCOMBOBOX_H__6C1728B4_F098_408B_919A_A8DA393AA426__INCLUDED_)
    //MyComboBox.cpp
    Code:
     
    // MyComboBox.cpp : implementation file
     
    //
     
     
    #include "stdafx.h"
     
    #include "TestColorCombo.h"
     
    #include "MyComboBox.h"
     
     
    #ifdef _DEBUG
     
    #define new DEBUG_NEW
     
    #undef THIS_FILE
     
    static char THIS_FILE[] = __FILE__;
     
    #endif
     
     
    /////////////////////////////////////////////////////////////////////////////
     
    // CMyComboBox
     
     
    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);
     
    }
    //MyDialog.h
    Code:
    #include "CMyComboBox.h"
    .
    ..... //here .... means necessary constructors() and all
    .....
    ....
    public :
    CMyComboBox  mybox;
    ......
    ....
    }
    #MyDialog.cpp
    Code:
    .......
    ....
    
    ...
    
    Bool MyDialog::OnInitDialog()
    {
    ........
    mybox.SetCursel(1); //DDX mapping is done by me
    
    .....
    return true;
    }
    Now result after compilation is ,dialog is not even loading, cursor rotates and nothing opens

  4. #4
    Join Date
    Oct 2019
    Posts
    82

    Re: Changing background Color of CBS_DROPDOWN CCombobox

    Anyone please suggest?

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Changing background Color of CBS_DROPDOWN CCombobox

    Quote Originally Posted by Beginner_MFC View Post
    I tried below code
    // MyComboBox.h
    Code:
    ...
    
    Bool MyDialog::OnInitDialog()
    {
    ........
    mybox.SetCursel(1); //DDX mapping is done by me
    
    .....
    return true;
    }
    Now result after compilation is ,dialog is not even loading, cursor rotates and nothing opens
    Did you debug your code?
    Is the MyDialog::OnInitDialog() called?
    Where is the call of the base class OnInitDialog()?
    Victor Nijegorodov

  6. #6
    Join Date
    Oct 2019
    Posts
    82

    Re: Changing background Color of CBS_DROPDOWN CCombobox

    Yes i debugged. OnInitDialog() is not called. From where it is supposed to called there dlg.DoModal() is returning -1

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Changing background Color of CBS_DROPDOWN CCombobox

    Then, please, post your real actual code, not a pseudo code you have posted.
    The best would be to post the small test project that can reproduce this behavior.
    Victor Nijegorodov

  8. #8
    Join Date
    Oct 2019
    Posts
    82

    Re: Changing background Color of CBS_DROPDOWN CCombobox

    Able to load and open dialog . But background color is not reflected. I placed the breakpoint at OnChildNotify() but it is not called. I check DDX mapping ,there memory is allocated to MyCComboBox variable

  9. #9
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Changing background Color of CBS_DROPDOWN CCombobox

    Your manner to not post your actual code (and instead post pseudo code or/and some meaningless "description") doesn't make much sense and does not help to understand what and how you really do.
    Victor Nijegorodov

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