none
如何改变Button的颜色 RRS feed

答案

  • 你重写CButton类,重写虚函数DrawItem();具体参考MSDN文档CButton类
    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    • 已标记为答案 A_Big_Mess 2011年4月1日 1:41
    2011年3月31日 3:54
    版主

全部回复

  • 在paint事件中重画。

    2011年3月30日 7:52
  • 重写CButton类,处理虚函数DrawItem

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


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年3月30日 8:48
    版主
  • 重写CButton类,处理虚函数DrawItem

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


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    你这个好像只是改变文件的颜色吧? lpDrawItemStruct这个应该怎么传?
    2011年3月31日 2:03
  • 你重写CButton类,重写虚函数DrawItem();具体参考MSDN文档CButton类
    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    • 已标记为答案 A_Big_Mess 2011年4月1日 1:41
    2011年3月31日 3:54
    版主