2012-09-21 38 views
0

MFC: 我读这个代码是绘制一个椭圆(不是固体内部),但我不明白为什么在这里需要两次函数“pDC-> Ellipse(...)”? (溶胶== 0,和do_what == DRAW_ELLIPSE)为什么函数Ellipse(...)在这里需要两次绘制椭圆?

void CMy078207017Dlg::OnLButtonUp(UINT nFlags, CPoint point) 
{ 
     flag = 0; 
    end = point; 
    assist = point; 
    if(p != NULL) 
    { 
     CDC* pDC = GetDC(); 
     CPen pen; 
     CBrush brush; 
     getpen(pen, pDC, col, bol); 
     if(do_what >= DRAW_LINE && do_what <= DRAW_RRECT) 
     { 
      p->p[0] = start; 
      p->p[1] = end; 
     } 

     if(sol == 1) 
     { 
      getbrush(brush, pDC, col); 
     } 

     if(do_what == DRAW_LINE) 
     { 
      pDC->MoveTo(start); 
      pDC->LineTo(end); 
     } 
     else 
     { 
      if(do_what == DRAW_ELLIPSE || do_what == DRAW_CIRCLE) 
      { 

       assist = start; 
       if(do_what == DRAW_CIRCLE) 
       { 
        assist.y = end.y - end.x + start.x; 
       } 


       pDC->SetROP2(R2_NOT); 
       pDC->Ellipse(start.x, assist.y, end.x, end.y); 


       pDC->SetROP2(R2_COPYPEN); 
       if(sol == 0) 
       { 
        pDC->SelectStockObject(NULL_BRUSH); 
       } 
       pDC->Ellipse(start.x, assist.y, end.x, end.y); 


       end = point; 
      } 

     } 
     ReleaseDC(pDC); 
    } 
    CDialog::OnLButtonUp(nFlags, point); 
} 

如果删除了第一次调用了pdc->省略号(...),该椭圆将黑色固体内部。 如果我将第二个调用删除到pDC-> Ellipse(...),则椭圆将永远不会绘制,但在鼠标左键启动时会消失。

对话框: 当移动鼠标: enter image description here 鼠标移动(笔为绿色)

当鼠标按钮弹起: enter image description here 鼠标按钮弹出(笔为绿色)

此外, CBrush的颜色是 “CBrush brush; pDC-> Ellipse(start.x,assist.y,end.x,end.y);”

   ... 
    else if(do_what==DRAW_RECT||do_what==DRAW_RRECT){ 

      pDC->SetROP2(R2_NOT); 
      if(do_what==DRAW_RECT) 
      { 
       pDC->Rectangle(start.x,start.y,end.x,end.y); 
      } 
      else if(do_what==DRAW_RRECT) 
      { 
       pDC->RoundRect(start.x,start.y,end.x,end.y,20,20); 
      } 


      pDC->SetROP2(R2_COPYPEN); 
      if(sol==0) 
      { 
       pDC->SelectStockObject(NULL_BRUSH); 
      } 
      if(do_what==DRAW_RECT) 
      { 
       pDC->Rectangle(start.x,start.y,point.x,point.y); 
      } 
      else if(do_what==DRAW_RRECT) 
      { 
       pDC->RoundRect(start.x,start.y,point.x,point.y,20,20); 
      } 
      end=point; 
     } 
      ... 
+0

请缩进代码,它很难阅读。 – unwind

回答

0

我终于摆脱了麻烦: 代码别处:

void CDraw2009303476Dlg::OnMouseMove(UINT nFlags, CPoint point) 
{ 
    if(flag == 1) 
    { 
     CDC* pDC = GetDC(); 
     CPen pen; 
     CBrush brush; 
     getPen(pen, pDC, col, bol); 

     if(sol == 1) 
     { 
      getBrush(brush, pDC, col); 
     } 
     if(type >= DRAW_LINE && type <= DRAW_RRECT) 
     { 
      pDC->SetROP2(R2_NOT); 
      if(type == DRAW_LINE) 
      { 
       p->drawLine(point, pDC); 
      } 
      else if(type == DRAW_ELLIPSE) 
      { 
       p->drawEllipse(point, pDC); 
      } 
      else if(type == DRAW_CIRCLE) 
      { 
       p->drawEllipse(point, pDC, 1); 
      } 
      else if(type == DRAW_RECT) 
      { 
       p->drawRect(point, pDC); 
      } 
      else if(type == DRAW_RRECT) 
      { 
       p->drawRect(point, pDC, 1); 
      } 
     } 
     ReleaseDC(pDC); 
    } 
    CDialog::OnMouseMove(nFlags, point); 
} 

所以,策略是:利用 “了pdc-> SetROP2(R2_NOT);”一次和“p-> drawEllipse(point,pDC,1);”两次在同一个地方保存原始像素以获得线条绘制效果。 最后调用“pDC-> SetROP2(R2_COPYPEN); p-> drawEllipse(point,pDC,1)”是我们真正需要查看省略号的。 谢谢你的帮助。

1

这是因为调用pDC->SetROP2(R2_NOT)的:

,当涉及到矩形的策略可能会更清晰。根据MSDN的说法,R2_NOT标志表示“像素保持不变”。您可以在这里阅读文档 - http://msdn.microsoft.com/en-us/library/99ax95h9.aspx

+0

但我在这里感到困惑:为什么当我删除第一次调用该函数时,椭圆内部会变黑。 – Al2O3

+0

删除第一个*哪个*函数的调用?你尝试过调试吗? –

+0

调用pDC->椭圆(...) – Al2O3

0

椭圆由当前画笔绘制,其内部填充当前画笔。

CDC::Ellipse()参考从MSDN

pDC->SetROP2(R2_NOT); 

// pDC->Ellipse(start.x,assist.y,end.x,end.y); 
pDC->SetROP2(R2_COPYPEN); 
if(sol==0){ 
      pDC->SelectStockObject(NULL_BRUSH); 
    } 
if(do_what==DRAW_CIRCLE){ 
      assist.y=point.y-point.x+start.x; 
    } 
pDC->Ellipse(start.x,assist.y,point.x,point.y); 

所以我们的ROP是R2_COPYPENstates,要使用的像素是当前画笔的颜色。我的猜测是,笔是黑色的,椭圆被填充为黑色(请参阅上面有关用于填充椭圆的笔刷的椭圆描述)。

pDC->SetROP2(R2_NOT); 

pDC->Ellipse(start.x,assist.y,end.x,end.y); 
pDC->SetROP2(R2_COPYPEN); 
if(sol==0){ 
      pDC->SelectStockObject(NULL_BRUSH); 
    } 
if(do_what==DRAW_CIRCLE){ 
      assist.y=point.y-point.x+start.x; 
    } 
// pDC->Ellipse(start.x,assist.y,point.x,point.y); 

因此,如果我们删除第二Ellipse呼叫然后我们使用R2_NOT,所以该像素保持相同(所以灰色背景),所以我们最终绘制用钢笔椭圆的颜色作为背景相同的,所以它从未见过。

你真的需要调试才能看到发生了什么,但是如果你能在每个点找到笔的颜色和笔刷颜色,你应该清楚发生了什么。

+0

我不擅长调试,但是我得到了笔的颜色,即RGB(0,255,0):绿色,但正如您所看到的,只有它的轮廓是绿色的,内部是黑色的。 – Al2O3

+0

我不知道是否“CBrush brush”使“pDC->椭圆(...)”绘制一个黑色内部的椭圆。 – Al2O3

+0

@JohnSon是的内部是黑色的,我敢打赌,如果你看看你的画笔的颜色,它可能是黑色的。如果您阅读了'pDC-> Ellipse()'[method](http://msdn.microsoft.com/zh-CN/library/6hkxb3kd%28v=vs.80%29.aspx)上的文档, ,它提到刷子颜色将用于填充椭圆的颜色。 – display101