首页 > C/C++开发工具专区 > VC技术 > VC画图用到的主要方法
2012
07-25

VC画图用到的主要方法

 

1。鼠标落下,记录鼠标的起始位置

void CMyEasyDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值
 //graph->m_nType=dlg->
 m_bStartDraw = true;
 m_PtPress = m_PtLast = point;

 CView::OnLButtonDown(nFlags, point);
}

2.鼠标抬起,画图结束,记录下画图的所有参数

oid CMyEasyDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{

 DrawDialog *dlg=CMainFrame::GetDrawTool();
 if (m_bStartDraw)
 {
  // 创建新图形对象
  EasyGraphics* graph = new EasyGraphics;

  // 对图形属性赋值
  graph->m_nType = m_nGraphType;
  graph->m_clrColor =dlg->m_clrColor;
  graph->m_nLineType = dlg->m_nType;
  graph->m_nLineWidth = dlg->m_lineWide;
  graph->m_PtTopLeft = m_PtPress;
  graph->m_PtBottomRight = point;

  // 添加到图形容器中
  m_GraphAry.Add(graph);

  // 一个绘制结束
  m_bStartDraw = false;

  // 让视图重绘
  Invalidate();
 }

 CView::OnLButtonUp(nFlags, point);
}

3.鼠标移动产生的动感

void CMyEasyDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
 if (m_bStartDraw)
 {
  CDC* pDC = GetDC();
  int nDrawMode = pDC->SetROP2(R2_NOTXORPEN);  // 设置为“异或模式”
  DrawDialog* dlg = CMainFrame::GetDrawTool();
  EasyGraphics* graph = new EasyGraphics;   // 临时绘制对象

  // 对图形属性赋值;
  graph->m_nType = m_nGraphType;
  graph->m_clrColor =dlg->m_clrColor;
  graph->m_nLineType = dlg->m_nType;
  graph->m_nLineWidth = dlg->m_lineWide;

  graph->m_PtTopLeft = m_PtPress;
  graph->m_PtBottomRight = m_PtLast;
  DrawGraphics(pDC, graph); // 擦除上次绘制图形

  graph->m_PtBottomRight = point;
  DrawGraphics(pDC, graph); // 绘制新图形

m_PtLast = point;   // 保存当前鼠标坐标
  pDC->SetROP2(nDrawMode);
  if(graph!=NULL)
  {
   delete graph;
   graph=NULL;
  }
  ReleaseDC(pDC);
 }

 CView::OnMouseMove(nFlags, point);
}

4.画图操作函数

void CMyEasyDrawView::DrawGraphics(CDC* pDC, EasyGraphics* graph)
{
 LOGBRUSH logBrush;
 logBrush.lbStyle = BS_SOLID;
 logBrush.lbColor = graph->m_clrColor;
 CPen pen(graph->m_nLineType |PS_GEOMETRIC|PS_ENDCAP_ROUND, 
    graph->m_nLineWidth, &logBrush);

 // 选中新的笔对象
 CPen* oldPen = pDC->SelectObject(&pen);
 
 // 使用透明画刷
 CBrush* oldBrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);

 switch (graph->m_nType)
 {
 case EG_RECT:  
  // 绘制矩形
  pDC->Rectangle(graph->m_PtTopLeft.x, 
        graph->m_PtTopLeft.y,
        graph->m_PtBottomRight.x,
        graph->m_PtBottomRight.y);
  break;

 case EG_CIRCLE:  
  // 绘制椭圆
  pDC->Ellipse(graph->m_PtTopLeft.x, 
      graph->m_PtTopLeft.y,
      graph->m_PtBottomRight.x,
      graph->m_PtBottomRight.y);
  break;

 default:   
  // 绘制直线
  pDC->MoveTo(graph->m_PtTopLeft);
  pDC->LineTo(graph->m_PtBottomRight);
 }

 pDC->SelectObject(oldPen);
 pDC->SelectObject(oldBrush);
}

5.初始设置

void CMyEasyDrawView::OnDraw(CDC* pDC/*pDC*/)
{
 CMyEasyDrawDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 if (!pDoc)
  return;

 // TODO: 在此处为本机数据添加绘制代码
 CRect rect;
 GetClientRect(rect);

 CDC memDC;
 CBitmap MemBitmap;
 //设备描述表初始话
 memDC.CreateCompatibleDC(NULL);
 //建立与屏幕显示兼容的内存显示设备
 MemBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
 //选取空白位图
 memDC.SelectObject(MemBitmap);
 memDC.FillSolidRect(100,0,rect.Width(),rect.Height(),RGB(255,0,0));
 EasyGraphics* graph=NULL;
 for(INT_PTR i=0;i<m_GraphAry.GetCount();i++)
 {
  graph=(EasyGraphics*)m_GraphAry[i];
  DrawGraphics(&memDC,graph);
 }
 pDC->BitBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,SRCCOPY);

}

FROM:http://anwj336.blog.163.com/blog/static/8941520920092792634991/


留下一个回复