Mfc CComboBoxEx-如何更改背景颜色 [英] Mfc CComboBoxEx - How to change the background color

查看:594
本文介绍了Mfc CComboBoxEx-如何更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个派生自CComboBoxEx的类,我正在尝试更改背景色.我以为它可以像ComboBox一样工作(使用SetBkColor函数),但是它不会更改背景色.

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.

这是我尝试过的:

    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;
         }

我也尝试过使用OnEraseBkgnd(),它也没有起作用.

I've tried with OnEraseBkgnd() too and it didn't worked either.

我是否需要对派生的CComboBox类进行子类化并在该类中设置背景颜色?

Do I need to subclass a derived CComboBox class and set the background color in that class?

谢谢.

推荐答案

我很惊讶您到目前为止获得的所有答案都建议在父窗口中处理WM_CTLCOLOR或使用OWNERDRAW样式之一.

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.

在父窗口中处理WM_CTLCOLOR意味着您需要在每个将要使用此类组合框的父窗口类中复制该代码.如果您想多次使用组合框,显然这是一个糟糕的解决方案.

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.

添加OWNERDRAW样式可能会影响您想继承的其他现有控件,并且您可能需要处理其他问题.这也不是一个完美的解决方案.

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);
}

这是组合框的外观:

如果要为边框和字形使用自定义颜色,则需要自己处理WM_PAINT.

If you want to have custom color for borders and glyph then you need to handle WM_PAINT yourself.

这篇关于Mfc CComboBoxEx-如何更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆