Mfc CComboBoxEx - 如何更改背景颜色
我有一个派生自 CComboBoxEx 的类,我正在尝试更改背景颜色.我在想它会像 ComboBox 一样工作(使用 SetBkColor 函数),但它不会改变背景颜色.
这是我尝试过的:
BEGIN_MESSAGE_MAP(CMyComboBoxEx, CComboBoxEx)ON_WM_CTLCOLOR()END_MESSAGE_MAP()无效 CMyComboBoxEx::SetBkColor(COLORREF 背景颜色){m_backgroundColor = 背景颜色;m_brBkgnd.DeleteObject();m_brBkgnd.CreateSolidBrush(backgroundColor);}HBRUSH CMyComboBoxEx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor){HBRUSH 画笔 = __super::OnCtlColor(pDC, pWnd, nCtlColor);pDC->SetBkColor(RGB(255,0,0));回刷;}
我也试过 OnEraseBkgnd()
也没有成功.
我是否需要继承一个派生的 CComboBox 类并在该类中设置背景颜色?
谢谢.
解决方案我很惊讶到目前为止你得到的所有答案都建议在父窗口中处理 WM_CTLCOLOR
或使用 OWNERDRAW
样式.
在父窗口中处理 WM_CTLCOLOR
意味着您需要在每个父窗口的类中复制该代码,您将在其中使用此类组合框.如果您想多次使用组合框,这显然是一个糟糕的解决方案.
添加 OWNERDRAW
样式可能会影响您想要子类化的其他现有控件,并且您可能需要处理其他问题.这也远不是一个简单的解决方案.
幸运的是,还有另一种解决方法 - 使用
如果您想为边框和字形自定义颜色,那么您需要自己处理 WM_PAINT.
I have a class that is derived from CComboBoxEx and I'm trying to change the background color. I was thinking that it would work like a ComboBox (using the SetBkColor function), but it doesn't change the background color.
Here's what I have tried :
BEGIN_MESSAGE_MAP(CMyComboBoxEx, CComboBoxEx)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
void CMyComboBoxEx::SetBkColor(COLORREF backgroundColor)
{
m_backgroundColor = backgroundColor;
m_brBkgnd.DeleteObject();
m_brBkgnd.CreateSolidBrush(backgroundColor);
}
HBRUSH CMyComboBoxEx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH brush = __super::OnCtlColor(pDC, pWnd, nCtlColor);
pDC->SetBkColor(RGB(255,0,0));
return brush;
}
I've tried with OnEraseBkgnd()
too and it didn't worked either.
Do I need to subclass a derived CComboBox class and set the background color in that class?
Thx.
解决方案I'm surprised that all the answers you've got so far suggest either handling WM_CTLCOLOR
in parent window or use one of OWNERDRAW
styles.
Handling WM_CTLCOLOR
in parent window means you'll need to duplicate that code in each parent window's class where you'll use such combobox. This is obviously a bad solution if you want to use combobox more than once.
Adding OWNERDRAW
style may have impact on other existing controls that you'd like to subclass and you may need to handle additional problems. That's also far from a sinmple solution.
Fortunately, there's another way to solve it - use Message Reflection. And all you need to do is to add ON_WM_CTLCOLOR_REFLECT()
entry to the message map and CtlColor
handler.
In case of combobox control I'd do it like this:
MyComboBoxEx.h
class CMyComboBoxEx : public CComboBoxEx
{
public:
CMyComboBoxEx();
virtual ~CMyComboBoxEx();
protected:
CBrush m_BkBrush;
DECLARE_MESSAGE_MAP()
public:
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
MyComboBoxEx.cpp
CMyComboBoxEx::CMyComboBoxEx()
{
m_BkBrush.CreateSolidBrush(RGB(0, 255, 0));
}
CMyComboBoxEx::~CMyComboBoxEx()
{
}
BEGIN_MESSAGE_MAP(CMyComboBoxEx, CComboBoxEx)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
HBRUSH CMyComboBoxEx::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetTextColor(RGB(255, 0, 0));
pDC->SetBkColor(RGB(0, 255, 0));
return m_BkBrush;
}
HBRUSH CMyComboBoxEx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
return CtlColor(pDC, nCtlColor);
}
Here's how such combobox looks:
If you want to have custom color for borders and glyph then you need to handle WM_PAINT yourself.
相关文章