SDI默认定义的背景颜色是白色,请问如何改变它的颜色?

解决方案 »

  1.   

    重载OnCtlColor函数
    详见msdn
      

  2.   

    在OnCtlColor可以写你想要的颜色的
      

  3.   

    也可以这样,也很方便
    void CMyView::OnDraw(CDC* pDC)
    {
    CSsDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    ///////////////////////////////////////////////////////////
    CRect rect;
    this->GetClientRect(rect);
    pDC->FillSolidRect(&rect,RGB(255,0,0));//设置你需要的颜色
    ////////////////////////////////////////////////////////////
    // TODO: add draw code for native data here
    }
      

  4.   

    一个是在注册窗口类的时候可以指定背景色;
    一个是在响应WM_ERASEBKGND消息(OnErasebkgnd)的时候绘制背景,可以贴图。
      

  5.   

    改变视窗的背景颜色的两种方法 
     
    方法 一:Windows 向窗口发送一个WM_ERASEBKGND 消息通知该窗口擦除背景,可以使用
    ClassWizard 重载该消息的缺省处理程序来擦除背景(实际是画),并返回TRUE 以防止
    Windows擦除窗口。
    //Paint area that needs to be erased.
    BOOL CSampleView : : OnEraseBkgnd (CDC* pDC)
    {
    // Create a pruple brush.
    CBrush Brush (RGB (128 , 0 , 128) );
    // Select the brush into the device context .
    CBrush* pOldBrush = pDC―>SelcetObject (&brush);
    // Get the area that needs to be erased .
    CRect reClip ;
    pDC―>GetCilpBox (&rcClip);
    //Paint the area.
    pDC―> PatBlt (rcClip.left , rcClip.top ,
    rcClip.Width ( ) , rcClip.Height ( ) , PATCOPY );
    //Unselect brush out of device context .
    pDC―>SelectObject (pOldBrush );
    // Return nonzero to half fruther processing .
    return TRUE;
    }
    方法二:调用CWinApp : : SetDialogBkColor 可以改变所有应用程序的背景颜色。第一个参数
    指定了背景颜色,第二个参数指定了文本颜色。下例将应用程序对话设置为蓝色背景和
    黄色文本。
    BOOL CSampleApp : : InitInstance ( )
    {

    //use blue dialog with yellow text .
    SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 , 255 , 0 ) ) ;

    }
    需要重画对话(或对话的子控件)时,Windows 向对话发送消息WM_CTLCOLOR,通常用
    户可以让Windows 选择绘画背景的刷子,也可重置该消息指定刷子。下例说明了创建一
    个红色背景对话的步骤。
    首先,给对话基类增加一人成员变量CBursh :
    class CMyFormView : public CFormView
    {

    private :
    CBrush m_ brush ; // background brush

    } ;
    其次, 在类的构造函数中将刷子初始化为所需要的背景颜色。
    CMyFormView : : CMyFormView ( )
    {
    // Initialize background brush .
    m_brush .CreateSolidBrush (RGB ( 0, 0, 255 ) )
    }
    最后,使用ClassWizard处理WM_CTLCOLOR消息并返回一个用来绘画对话背景的刷子句
    柄。注意:由于当重画对话控件时也要调用该函数,所以要检测nCtlColor参量。
    HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor )
    {
    // Determine if drawing a dialog box . If we are , return +handle to
    //our own background brush . Otherwise let windows handle it .
    if (nCtlColor = = CTLCOLOR _ DLG )
    return (HBRUSH) m_brush .GetSafeHandle ( ) ;
    return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor );
    }  
      

  6.   

    呵呵,晓得滴都被说完达PS:SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 , 255 , 0 ) ) ;这东东在VS2003下已经不能用了重载OnCtlColor吧
      

  7.   

    谢谢大家关心,问题解决了,各位的意见都可以实现。但是我觉得
    最好的方法是在onprecreatewindow中改变注册的窗口类,也就是采用自己定义的窗口类的类名。
    我是这样实现的:
    cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,0,(HBRUSH)GetStockObject(LTGRAY_BRUSH),0);
    采用背景画刷为LTGRAY_BRUSH的窗口类注册。