用OnCtlColor消息方法不能改变按钮的背景色。
听说可以重写类,或者自绘按钮。
请大家讲讲具体的步骤。

解决方案 »

  1.   

    1.重写Button类,继承自CButton
    2.实现DrawItem函数
    3.使用BS_OWNERDRAW自绘风格动态创建,或使用Attach函数代替原来按钮的窗口过程BOOL   XButton::Attach(const UINT nID, CWnd* pParent) 

            GetDlgItem(nID)->ModifyStyle(0, BS_OWNERDRAW, 0); 
            if ( !SubclassDlgItem(nID, pParent) ) 
                  return FALSE; 
            return TRUE; 
    } InitDialog()中加入m_Btn.Attach(IDC_BTN_TEST, this);
      

  2.   

    有两种方法:
    第一种就是:通过添加WM_CTLCOLOR这个消息,在这个消息的消息响应函数中去修改按钮的背景色;
    先在对话框中添加:HBRUSH hBrush;在构造函数中对hBrush进行创建:hBrush=CreateSolidBrush(m_hWnd,自定义颜色);
    HBRUSH CDialogColor::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        HBRUSH hbr = CDialog::OnCtlColor(pDC,pWnd, nCtlColor);
        if(pWnd->GetDlgCtrlID() == 按钮的ID)
        {
            pDC->SetBkMode(TRANSPARENT);
            return (HBRUSH)hBrush;
         }
         return hbr;
    }
    第二种方法:
    1.重新继承一个基于CButton类的新类CMyButton
    2.在类中重载PreSubclassWindow()和DrawItem(..)这两个函数...
    3.在PreSubclassWindow()中添加代码:ModifyStyle(0,BS_OWNERDRAW);
      在DrawItem(...)函数中什么都不用添加;
    4.添加WM_PAINT消息.在该消息的消息响应函数中添加画按钮的代码:
      如:CRect rcClient;
          GetClientRect(rcClient);
          dc.FillSolidRect(rcClient,自定义颜色);
          CString strText;
          GetWindowText(strText);
          dc.SetBkMode(TRANSPARENT);
          dc.TextOut(rcClient.left,rcClient.top,strText);  //按钮有文本的一定要记得加上文本的显示
           CPen pen(PS_SOLID,1,RGB(0,0,0));   //定义一个画按钮边框的画笔;
          CPen *OldPen=dc.SelectObject(&pen);
          //画按钮的边框
          dc.MoveTo(rcClient.left,rcClient.top);    
          dc.LineTo(rcClient.left,rcClient.bottom);
          dc.MoveTo(rcClient.left,rcClient.bottom);
          dc.LineTo(rcClient.right,rcClient.bottom);
          dc.MoveTo(rcClient.right,rcClient.bottom);
          dc.LineTo(rcClient.right,rcClient.top);
          dc.MoveTo(rcClient.right,rcClient.top);
          dc.LineTo(rcClient.left,rcClient.top);
    如果觉得第二个方法有点不是很理解,我这有个通过自绘的方式去实现的仿360按钮的代码,可以参考参考;
    http://download.csdn.net/detail/allen_lanyuhai/3658158
      

  3.   

    http://blog.csdn.net/orbit/article/details/485125
    http://download.csdn.net/detail/monkey708/3180759
      

  4.   

    重写CButton类,添加处理虚函数DrawItem,MSDN上的例子程序// NOTE: CMyButton is a class derived from CButton. The CMyButton
    // object was created as follows:
    //
    // CMyButton myButton;
    // myButton.Create(_T("My button"), 
    //      WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW, 
    //      CRect(10,10,100,30), pParentWnd, 1);
    //// This example implements the DrawItem method for a CButton-derived 
    // class that draws the button's text using the color red.
    void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
       UINT uStyle = DFCS_BUTTONPUSH;   // This code only works with buttons.
       ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);   // If drawing selected, add the pushed style to DrawFrameControl.
       if (lpDrawItemStruct->itemState & ODS_SELECTED)
          uStyle |= DFCS_PUSHED;   // Draw the button frame.
       ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, 
          DFC_BUTTON, uStyle);   // Get the button's text.
       CString strText;
       GetWindowText(strText);   // Draw the button text using the text color red.
       COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
       ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
          &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
       ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
    }
      

  5.   

    LZ愚昧,还是搞不定这个问题。哪位大仙如果肯发给现成的工程及对应的理论讲解到[email protected] ,不胜感激,100分双手奉上。