怎么更改按钮CButton控件的颜色?

解决方案 »

  1.   

    请给与详细的解释,偶是新手阿,
    CButtonST是什么啊
      

  2.   

    //重载消息WM_CTRLCOLORCWnd::OnCtlColor  
    afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );pDCContains a pointer to the display context for the child window. May be temporary.pWndContains a pointer to the control asking for the color. May be temporary.nCtlColorContains one of the following values, specifying the type of control: CTLCOLOR_BTN   Button control
    CTLCOLOR_DLG   Dialog box
    CTLCOLOR_EDIT   Edit control
    CTLCOLOR_LISTBOX   List-box control
    CTLCOLOR_MSGBOX   Message box
    CTLCOLOR_SCROLLBAR   Scroll-bar control
    CTLCOLOR_STATIC   Static control 
    Example// 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;
    }
      

  3.   

    1。在对话框中加入一个成员变量:
    CBrush m_Brush;在OnInitDialog() 中初始化它:
    m_Brush.CreateSolidBrush(RGB(255, 255, 255));2。
    HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {

    if (nCtlColor==CTLCOLOR_BTN)
    {
    pDC->SetBkColor(RGB(255,05,0));
             
    return m_Brush;
    }
    }
      

  4.   

    按钮上显示自定义的ICON图也可以实现呀
    m_myButton.SetIcon(AfxGetApp()->LoadIcon(IDI_MYCOLOR));