我做了个测试,创建了一个单文档应用程序,在菜单中增加一项"图象",,在此菜单下增加一个选项"设置背景色",并定义该选项的id为ID_BC_Clr.然后,用Class Wizard定义此选项对应的函数为:OnBCClr()并编码如下:
void CTestView::OnBCClr() 
{
// TODO: Add your command handler code here
CColorDialog dlg;
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); if(dlg.DoModal()==IDOK)
{ COLORREF newclr = dlg.GetColor();
  
  if(m_color!=newclr)
  { m_color = newclr;
    pDoc->UpdateAllViews(this);
  }

}
}然后,又在CTestView类的void CTestView::OnDraw(CDC* pDC)中添加了代码:
使void CTestView::OnDraw(CDC* pDC)如下:
void CTestView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here CBrush brush,*poldbrush;
brush.CreateSolidBrush(m_color); poldbrush=pDC->SelectObject(&brush); CRect rect;
         pDC->Rectangle(rect); pDC->SelectObject(poldbrush);
}
我的理解,在我的void CTestView::OnBCClr() 中创建了一个设置色彩的.当选ok后,记录所选颜色.并更新.
再通过void CTestView::OnDraw(CDC* pDC)达到设置颜色的结果.
因为OnDraw是当view有更改时自动调用的,那么为什么我的代码不成功??

解决方案 »

  1.   

    1。 CRect rect;
             pDC->Rectangle(rect);  --->rect没有赋值,当然画不出来2。你应该在OnEraseBkgnd里面做
      

  2.   

    我即便加入:
        GetClientRect(rect);
        rect.DeflateRect(rect.Width(),rect.Height());还是不解决根本问题.无法通过调色版CColorDialog dlg;设置新的颜色!问题的根本到底在?????
      

  3.   

    现在我找到了点眉目!  不过还需要大家帮我,教我一下.我将原来 void CTestView::OnBCClr()的函数改变,不创建在View类下,而是改到void CTestDoc::OnBCClr() 既Doc类中,
    函数中的代码没有变!
    成员变量 COLORREF m_color也定义到Doc类中.
    void CTestDoc::OnBCClr() 
    {
    // TODO: Add your command handler code here
    CColorDialog dlg;
    CTestDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc); if(dlg.DoModal()==IDOK)
    { COLORREF newclr = dlg.GetColor();
      
      if(m_color!=newclr)
      { m_color = newclr;
        pDoc->UpdateAllViews(this);
      }

    }
    }下面的void CTestView::OnDraw(CDC* pDC)没有变!void CTestView::OnDraw(CDC* pDC)
    {
    CTestDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here CBrush brush,*poldbrush;
    brush.CreateSolidBrush(m_color); poldbrush=pDC->SelectObject(&brush); CRect rect;
             GetClientRect(rect);
             rect.DeflateRect(rect.Width(),rect.Height());
             pDC->Rectangle(rect); pDC->SelectObject(poldbrush);
    }这样的话,,就成功了!!!我现在唯一的不理解在于!
    同样的OnBCClr() 函数放在View类下,整个程序不成功,
    改到Doc类中就可以了,这是为什么????我怎么理解,什么函数放到什么类中??
      

  4.   

    到底为什么要把设置背景颜色的函数放在Doc类中?放在View类中就会有不刷新的问题????