如题,让一个按钮变成红色或者绿色 最好有代码

解决方案 »

  1.   

    http://hi.baidu.com/fox000002/blog/item/0c2892091fba44206a60fb72.html
      

  2.   

    http://www.codeguru.com/cpp/controls/buttonctrl/article.php/c5169/Colored-Buttons.htm
      

  3.   

    if(button_id == 1)
    {
    RECT m_rect;
    m_button3.GetClientRect(&m_rect); static bool b = false;   
    CBrush bru(RGB(255,0,0));   
    CPaintDC *dc;
    dc=(CPaintDC*)m_button3.GetDC();   
       
    CString str;   
    GetDlgItemText(IDC_BUTTON1,str);   
    dc->SetBkMode(TRANSPARENT);    if(b==false)   
    {   
    dc->FillRect(&m_rect,&bru);   
    dc->DrawText(str,&m_rect,DT_CENTER | DT_SINGLELINE | DT_VCENTER );   
    b=true;   
    return;   
    }
    }
      

  4.   

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