【问题标题】:Changing the background color of an MFC dialog element更改 MFC 对话框元素的背景颜色
【发布时间】:2014-07-28 19:53:33
【问题描述】:

我有一个基于 MFC 对话框的程序,其中包含多个元素。我正在使用带有 SP1 的 VS2010 Professional 在 Windows 7 上进行开发。我想更改一些滑块元素的背景颜色(来自CSliderCtrl 类)。我发现的唯一一件事是尝试覆盖 CSliderCtrl 的 OnCtlColor 函数。我通过以下方式做到了这一点:

class MySlider : public CSliderCtrl
{
public:
    MySlider(UINT r, UINT g, UINT b){R=r;G=g;B=b;}
    virtual ~MySlider(){}

    UINT R;
    UINT G;
    UINT B;

    HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        pDC->SetBkColor(RGB(R, G, B));

        return CSliderCtrl::OnCtlColor(pDC, pWnd, nCtlColor);
    }
};

然后,我将所有 CSliderCtrl 元素替换为 MySlider 元素,并将所需的背景 rgb 值传递给构造函数。然而,这并没有奏效。

谁能帮我弄清楚如何正确设置滑块元素的背景颜色? (或与此相关的任何其他元素)

【问题讨论】:

标签: c++ visual-studio-2010 background mfc element


【解决方案1】:

使用背景颜色创建一个画笔并返回该 HBRUSH 以获取颜色变化。

HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    CSliderCtrl::OnCtlColor(pDC, pWnd, nCtlColor);

    pDC->SetBkColor(RGB(R, G, B));
    static CBrush br(RGB(R, G, B));

    return (HBRUSH)br;
}

【讨论】:

    【解决方案2】:

    覆盖 OnPaint 并绘制一个实心矩形

    void MySlider::OnPaint() 
    {
        CPaintDC dc(this); // device context for painting
    
        RECT rect ;
    
        CRect rectButton;
        this->GetWindowRect(&rectButton);
    
        COLORREF cr = RGB(60,180,80)
        dc.FillSolidRect(&rect, cr); // Background color
    
    
            // Any other drawing
    }
    

    【讨论】:

    • cdc 从何而来?
    • 我怀疑 cdc 是 dc 的拼写错误,声明为 CPaintDC(this)
    猜你喜欢
    • 2013-06-07
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多