void CMyView::Draw()
{ static int i;
  float x1=100,y1=100,x2=600,y2=600;
  CRect rect((int)x1,(int)y1,(int)x2,(int)y2);
  float xLen=x2-x1,yLen=y2-y1;
  CClientDC dc(this);
    GetClientRect(&rect);//第一至第九个正方形的设定
  re1.top=re2.top=re3.top=rect.top;
  re8.top=re9.top=re4.top=xLen/3.0;
  re7.top=re6.top=re5.top=2.0*yLen/3.0;  re1.right=re8.right=re7.right=xLen/3.0;
  re2.right=re9.right=re6.right=2*xLen/3.0;
  re3.right=re4.right=re5.right=rect.right;  re1.left=re8.left=re7.left=rect.left;
  re2.left=re9.left=re6.left=xLen/3.0;
  re3.left=re4.left=re5.left=2.0*xLen/3.0;  re1.bottom=re2.bottom=re3.bottom=xLen/3.0;
  re8.bottom=re9.bottom=re4.bottom=2.0*xLen/3.0;
  re7.bottom=re6.bottom=re5.bottom=rect.bottom;*/
  CRect re[9];
  re[0].top=re[1].top=re[2].top=rect.top;
  re[7].top=re[8].top=re[3].top=(int)(yLen/3);
  re[6].top=re[5].top=re[4].top=(int)(2*yLen/3.0);  re[0].right=re[7].right=re[6].right=int(xLen/3.0);
  re[1].right=re[8].right=re[5].right=int(2*xLen/3.0);
  re[2].right=re[3].right=re[4].right=rect.right;  re[0].left=re[7].left=re[6].left=rect.left;
  re[1].left=re[8].left=re[5].left=int(yLen/3.0);
  re[2].left=re[3].left=re[4].left=int(2.0*yLen/3.0);  re[0].bottom=re[1].bottom=re[2].bottom=int(yLen/3.0);
  re[7].bottom=re[8].bottom=re[3].bottom=(int)(2*yLen/3.0);
  re[6].bottom=re[5].bottom=re[4].bottom=rect.bottom;
  //画图形
  CBrush br[9];
  br[0].CreateSolidBrush(RGB(255,0,0));
  br[1].CreateSolidBrush(RGB(0,255,0));
  br[2].CreateSolidBrush(RGB(0,0,255));
  br[3].CreateSolidBrush(RGB(200,20,0));
  br[4].CreateSolidBrush(RGB(200,0,80));
  br[5].CreateSolidBrush(RGB(180,90,0));
  br[6].CreateSolidBrush(RGB(150,0,40));
  br[7].CreateSolidBrush(RGB(90,100,0));
  br[8].CreateSolidBrush(RGB(50,10,170));
  for(i=0;;)
  {
  dc.SelectObject(&br[i]);
  dc.Rectangle(&re[i]);
  ++i;
   i=i%9;
}
 问题就是在画图时显示的是整个客户区图形,而我本只想在rect所指定的正方形中画图,即把整个rect( ((int)x1,(int)y1,(int)x2,(int)y2)分成9块,请大家给我指点一下,先谢谢各位好友啦。
    

解决方案 »

  1.   

    那就分割成9块吧 CsplitterWnd
      

  2.   

    9个方块分得不对,另外:
    for(i=0;i<9;i++)
    {//不能死循环
    dc.SelectObject(&br[i]);
    dc.Rectangle(&re[i]);
    ++i;
    }
      

  3.   


    void CXXView::OnDraw(CDC* pDC)
    {
    CCDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    int i = 0;
    int x1=100,y1=100,x2=600,y2=600; CRect rect((int)x1,(int)y1,(int)x2,(int)y2);

    int xLen=(x2-x1)/3, yLen=(y2-y1)/3; CRect re[9];
    int nCol = 0;
    int nRow = 0;
    for(i=0; i<9 ;i++)
    {
    nCol = i / 3; nRow = i % 3; re[i].top = rect.top + nCol * yLen;
    re[i].bottom = re[i].top + yLen; re[i].left = rect.left + nRow * xLen;
    re[i].right = re[i].left + xLen;
    } CBrush br[9];
    br[0].CreateSolidBrush(RGB(255,0,0));
    br[1].CreateSolidBrush(RGB(0,255,0));
    br[2].CreateSolidBrush(RGB(0,0,255));
    br[3].CreateSolidBrush(RGB(200,20,0));
    br[4].CreateSolidBrush(RGB(200,0,80));
    br[5].CreateSolidBrush(RGB(180,90,0));
    br[6].CreateSolidBrush(RGB(150,0,40));
    br[7].CreateSolidBrush(RGB(90,100,0));
    br[8].CreateSolidBrush(RGB(50,10,170));
    for(i=0; i<9; i++)
    {
    pDC->SelectObject(&br[i]);
    pDC->Rectangle(&re[i]);
    }
    }