我的代码:
HBRUSH CStylebtnDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CBrush m_brush;  if(pWnd->GetDlgCtrlID()==IDC_LINE_WIDTH) 

pDC->SetTextColor(RGB(255,0,0)); 
pDC->SetBkColor(RGB(0,0,255)); 
return m_brush; 

// TODO: Return a different brush if the default is not desired
return hbr;
}
但不起效果!

解决方案 »

  1.   

    这样修改的只是按钮上的文字颜色和文字区域的背景色。
    至于按钮的颜色修改,可以去vckbase网站下载颜色按钮的类,通过自绘制的方式实现
      

  2.   

    我是写了集成了CButton类的类,然后重载了OnEraseBkgnd方法和DrawItem方法class CColorButton : public CButton
    {
    DECLARE_DYNAMIC(CColorButton)public:
    void SetBackColor(COLORREF cr); //设置按钮的背景颜色
    private:
    COLORREF m_BackColor; //按钮背景颜色
    public:
    BOOL   OnEraseBkgnd(CDC*   pDC);

            CColorButton();
    virtual ~CColorButton();protected:
    DECLARE_MESSAGE_MAP()
    };#include "ColorButton.h"
    // CColorButtonIMPLEMENT_DYNAMIC(CColorButton, CButton)CColorButton::CColorButton()
    {
    m_BackColor=RGB((255),(255),(255));
    }CColorButton::~CColorButton()
    {
    }void CColorButton::SetBackColor(COLORREF cr)
    {
    BackColor=cr;
    CDC   *dc=GetDC();   
    CRect   rect;   
    this->GetClientRect(&rect);   
    dc->FillRect(&rect,CBrush::FromHandle(CreateSolidBrush(m_BackColor)));
    }BOOL CColorButton::OnEraseBkgnd(CDC*   pDC)     
    {      
    CDC   *dc=GetDC();   
    CRect   rect;   
    this->GetClientRect(&rect);   
    dc->FillRect(&rect,CBrush::FromHandle(CreateSolidBrush(m_BackColor)));   
    return   true;   
    } BEGIN_MESSAGE_MAP(CColorButton, CButton)
    //{{AFX_MSG_MAP(My)   
    ON_WM_ERASEBKGND()   
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    然后把按钮添加为CColorButton类的变量,就可以了。
      

  3.   

    补充,需要在属性窗口中将按钮的OwnDraw属性设为true。
      

  4.   

    注意对比下,msdn上的例子
    // This OnCtlColor handler will change the color of a static control
    // with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog
    // class has an initialized and created CBrush member named m_brush.

    // The control will be painted with red text and a background
    // color of m_brush.HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
       // Call the base class implementation first! Otherwise, it may
       // undo what we're trying to accomplish here.
       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);   // Are we painting the IDC_MYSTATIC control? We can use
       // CWnd::GetDlgCtrlID() to perform the most efficient test.
       if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
       {
          // Set the text color to red
          pDC->SetTextColor(RGB(255, 0, 0));      // Set the background mode for text to transparent 
          // so background will show thru.
          pDC->SetBkMode(TRANSPARENT);      // Return handle to our CBrush object
          hbr = m_brush;   }   return hbr;
    }
    你的m_brush刚刚定义,什么都没有,另外,也怀疑局部cbrush对象是否恰当
      

  5.   

    用CButtonST
    在codeproject上有
      

  6.   

    按照2楼的方法
    用ccolorbutton定义按钮的时候出现:这是怎么回事,
    头文件我都包含了呀!!!
      

  7.   

    OnCtlColor不好使
    只是改变颜色的话可以考虑用CBitmapButtonCBitmapButton  m_BitmapBtn;
    m_BitmapBtn.LoadBitmaps(IDB_BITMAPUP,IDB_BITMAPDOWN,IDB_BITMAPFOCUS,IDB_BITMAPDISABLE); 
    m_BitmapBtn.SubclassDlgItem(IDC_BUTTON_LENGTH,this);
      

  8.   

    上面IDB_BITMAPUP,...等为按钮相应状态的位图
    IDC_BUTTON_LENGTH为按钮ID
      

  9.   

    CBitmapButton     m_BitmapBtn; 
    m_BitmapBtn.LoadBitmaps(IDB_BITMAPUP,NULL);   
    m_BitmapBtn.SubclassDlgItem(IDC_CESHI,this); 
    m_BitmapBtn.SizeToContent();
    但是按钮看不见啊
    我将它属性设为owner draw
      

  10.   

    属性设为owner draw,按钮失效了
      

  11.   

    将按钮的属性设置为OwnDraw之后,按钮就需要自己绘制了。
    绘制背景颜色使用我上面的代码,文字的话,应该在DarwItem里面绘制,不过我那段代码里面没有写出DrawItem的代码来,因为我写那个类的时候不需要输出文字,只需要表现颜色。如果楼主需要的话,可以留一个邮箱,我把完整的代码发给你。