-
May 27th, 2020, 12:01 PM
#1
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.
-
May 27th, 2020, 12:37 PM
#2
Re: Changing background Color of CBS_DROPDOWN CCombobox
Did you try to handle WM_CTLCOLOR?
Also see this thread.
Victor Nijegorodov
-
May 27th, 2020, 03:20 PM
#3
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
-
May 27th, 2020, 11:31 PM
#4
Re: Changing background Color of CBS_DROPDOWN CCombobox
-
May 28th, 2020, 01:50 AM
#5
Re: Changing background Color of CBS_DROPDOWN CCombobox
 Originally Posted by Beginner_MFC
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
-
May 28th, 2020, 02:58 AM
#6
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
-
May 28th, 2020, 03:09 AM
#7
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
-
May 28th, 2020, 10:16 AM
#8
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
-
May 28th, 2020, 10:25 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|