鼠标单击变换图形颜色

 1. 保持视图状态 m_rectEllipse(0,0,200,200)和 m_nColor (当前颜色);

 2.左键单击触发事件

  3.OnLButtonDown 可以改变m_nColor 的值但是 OnDraw 除了用户改变视图大小才会调用  所有必须调用 invalidateRect (从CWid继承的) 能够触发WM_PAINT 消息 引起OnDraw的调用

  4.判断点是不是在矩形内  if(m_rectEllipse.PtInRect(point)){}

 

 

1.private:
  int m_nColor;
  CRect m_rectEllipse;

 

2.

CEX05aView::CEX05aView():m_rectEllipse(0,0,200,200)
{
 // TODO: add construction code here
    m_nColor =GRAY_BRUSH;

}

 

3.

void CEX05aView::OnDraw(CDC* pDC)
{
 CEX05aDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);

      pDC->SelectStockObject(m_nColor);
   pDC->Ellipse(m_rectEllipse);

 // TODO: add draw code for native data here
}

 

4.

void CEX05aView::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 
 CView::OnLButtonDown(nFlags, point);
/**if(m_rectEllipse.PtInRect(point))/*判断的是矩形区域*

 
  if(m_nColor==GRAY_BRUSH)
 { m_nColor=WHITE_BRUSH;}
  else
 {m_nColor= GRAY_BRUSH;}
  InvalidateRect(m_rectEllipse);
  
}*/
CRgn rgn;
rgn.CreateEllipticRgnIndirect(m_rectEllipse);
if(rgn.PtInRegion(point))/*判断的是椭圆区域*/

 
  if(m_nColor==GRAY_BRUSH)
 { m_nColor=WHITE_BRUSH;}
  else
 {m_nColor= GRAY_BRUSH;}
  InvalidateRect(m_rectEllipse);
  
}
}

你可能感兴趣的:(图形,construction)