1,CBrush m_Brush
2,m_Brush.CreateSolidBrush(RGB(255,150,150));
3,HBRUSH CYourDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
  return (HBRUSH)m_Brush.GetSafeHandle();
}

解决方案 »

  1.   

    SetDialoBkColor()函数只有在App类里才能用。在其他地方不行。
    要改变背景色,可以下面两种
    HBRUSH CShenDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT              nCtlColor) 
          {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO: Change any attributes of the DC here
    hbr=CreateSolidBrush(RGB(0,255,0));//创建对话框的底色 // TODO: Return a different brush if the default is not desired
    return hbr;
          }
    或者
           BOOL CMyAppDlg::OnEraseBkgnd(CDC* pDC) 
        {
                  CRect rect;
            GetClientRect(&rect);      CBrush m_brush(RGB(255,0,0));        pDC->FillRect(&rect,&m_brush);         return FALSE;
           }
      

  2.   

    见《Visul C++ 技术内幕》第四版第109页有详细介绍。
      

  3.   

    //控件
    // 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 are 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;
    }CWnd::OnEraseBkgnd
    This method is called by the framework when the CWnd object background needs erasing. For example, when a window is resized. It is called to prepare an invalidated region for painting.afx_msg BOOL OnEraseBkgnd( 
    CDC* pDC ); 
    跟上面差不多,记得要返回true值