CDLine.cpp
上传用户:qiye66671
上传日期:2009-12-10
资源大小:182k
文件大小:8k
源码类别:

绘图程序

开发平台:

C/C++

  1. #include "stdafx.h"
  2. #include"CDLine.h"
  3. #include"EastDrawView.h"
  4. IMPLEMENT_SERIAL(CDLine,CUnit,1)
  5. void CDLine::DrawStatic(CDC*pDC)
  6. {
  7.  
  8.  CPen m_pen;
  9.  m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  10.  CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  11.  int oldDrawingMode=pDC->SetROP2(m_DrawingMode);
  12.  COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  13.  int OldBkMode=pDC->SetBkMode(m_BkMode);
  14.  pDC->MoveTo(m_FirstPoint);
  15.  pDC->LineTo(m_SecondPoint);
  16.  pDC->SelectObject(pen);
  17.  pDC->SetROP2(oldDrawingMode);
  18.  pDC->SetBkMode(m_BkMode);
  19.  pDC->SetBkColor(OldColor);
  20. }
  21. CDLine::CDLine()
  22. CUnit::Initial();
  23.    
  24. }
  25. CRgn* CDLine::GetRgn()
  26. {
  27.   if(m_Rgn==NULL)
  28.   
  29.    m_Rgn=new CRgn; 
  30.    else m_Rgn->DeleteObject();
  31.    {
  32.    CPoint point[4];
  33.    CPoint FirstPoint;
  34.    CPoint SecondPoint;
  35.   if((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.y-m_SecondPoint.y)<0)
  36.   {
  37.    if(m_FirstPoint.x>m_SecondPoint.x)
  38.    { 
  39.    
  40.    FirstPoint=m_SecondPoint;
  41.        SecondPoint=m_FirstPoint;   
  42.    }
  43.    else
  44.    {
  45.        FirstPoint=m_FirstPoint;
  46.        SecondPoint=m_SecondPoint;
  47.    
  48.    }
  49.    FirstPoint.Offset(-4-m_PenWidth,4+m_PenWidth);
  50.    SecondPoint.Offset(4+m_PenWidth,4+m_PenWidth);
  51.   point[0].x=FirstPoint.x-3-m_PenWidth;
  52.   point[0].y=FirstPoint.y-4-m_PenWidth;
  53.   point[1].x=FirstPoint.x+3+m_PenWidth;
  54.   point[1].y=FirstPoint.y+4+m_PenWidth;
  55.   point[2].x=SecondPoint.x+3+m_PenWidth;
  56.   point[2].y=SecondPoint.y+4+m_PenWidth;
  57.   point[3].x=SecondPoint.x-3-m_PenWidth;
  58.   point[3].y=SecondPoint.y-4-m_PenWidth;
  59.   
  60.   }
  61.   if((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.y-m_SecondPoint.y)>0)
  62.   {
  63.    if(m_FirstPoint.x>m_SecondPoint.x)
  64.    { 
  65.    
  66.    FirstPoint=m_FirstPoint;
  67.        SecondPoint=m_SecondPoint;   
  68.    }
  69.    else
  70.    {
  71.    FirstPoint=m_SecondPoint;
  72.        SecondPoint=m_FirstPoint; 
  73.    }
  74.     FirstPoint.Offset(-4-m_PenWidth,-4-m_PenWidth);
  75. SecondPoint.Offset(4+m_PenWidth,4+m_PenWidth);
  76.   point[0].x=FirstPoint.x+3+m_PenWidth;
  77.   point[0].y=FirstPoint.y-4-m_PenWidth;
  78.   point[1].x=FirstPoint.x-3-m_PenWidth;
  79.   point[1].y=FirstPoint.y+4+m_PenWidth;
  80.   point[2].x=SecondPoint.x-3-m_PenWidth;
  81.   point[2].y=SecondPoint.y+4+m_PenWidth;
  82.   point[3].x=SecondPoint.x+3+m_PenWidth;
  83.   point[3].y=SecondPoint.y-4-m_PenWidth;
  84.   
  85.   }
  86. if((m_FirstPoint.x-m_SecondPoint.x)==0)
  87.   {
  88.   point[0].x=m_FirstPoint.x-3-m_PenWidth;
  89.   point[0].y=m_FirstPoint.y-4-m_PenWidth;
  90.   point[1].x=m_FirstPoint.x+3+m_PenWidth;
  91.   point[1].y=m_FirstPoint.y-4-m_PenWidth;
  92.   point[2].x=m_SecondPoint.x+3+m_PenWidth;
  93.   point[2].y=m_SecondPoint.y+4+m_PenWidth;
  94.   point[3].x=m_SecondPoint.x-3-m_PenWidth;
  95.   point[3].y=m_SecondPoint.y+4+m_PenWidth;
  96.   
  97.   }
  98. if((m_FirstPoint.y-m_SecondPoint.y)==0)
  99.   {
  100.   point[0].x=m_FirstPoint.x-4-m_PenWidth;
  101.   point[0].y=m_FirstPoint.y-3-m_PenWidth;
  102.   point[1].x=m_FirstPoint.x-4-m_PenWidth;
  103.   point[1].y=m_FirstPoint.y+3+m_PenWidth;
  104.   point[2].x=m_SecondPoint.x+4+m_PenWidth;
  105.   point[2].y=m_SecondPoint.y+3+m_PenWidth;
  106.   point[3].x=m_SecondPoint.x+4+m_PenWidth;
  107.   point[3].y=m_SecondPoint.y-3-m_PenWidth;
  108.   
  109.   }
  110.   m_Rgn->CreatePolygonRgn(point,4,ALTERNATE);
  111.   }
  112.   return m_Rgn;
  113. }
  114. void CDLine::Initial()
  115. {
  116.   m_FirstPoint=0;
  117.   m_SecondPoint=0;
  118.   m_PenColor=RGB(0,0,0);
  119.   m_PenWidth=1;
  120.   m_PenStyle=PS_SOLID;
  121.   
  122.  
  123. }
  124. int CDLine::IsOnMarginPoint(CPoint point)
  125. {
  126.         CRect L_Rect1;
  127. CRect L_Rect2;
  128. CRect L_Rect3;
  129. L_Rect1.SetRect(m_FirstPoint,m_FirstPoint);
  130.         L_Rect2.SetRect(m_SecondPoint,m_SecondPoint);
  131. L_Rect3.SetRect((m_SecondPoint.x+m_FirstPoint.x)/2,(m_SecondPoint.y+m_FirstPoint.y)/2,(m_SecondPoint.x+m_FirstPoint.x)/2,(m_SecondPoint.y+m_FirstPoint.y)/2);
  132.         L_Rect1.InflateRect(5+m_PenWidth,5+m_PenWidth);
  133. L_Rect2.InflateRect(5+m_PenWidth,5+m_PenWidth);
  134. L_Rect3.InflateRect(5+m_PenWidth,5+m_PenWidth); 
  135. if(L_Rect1.PtInRect(point))
  136. {   
  137.     m_FoundPoint=m_FirstPoint;
  138. return 1;
  139. }
  140.  if(L_Rect2.PtInRect(point))
  141.  {   
  142.  m_FoundPoint=m_SecondPoint;
  143.  return 2;
  144.  }
  145.  if(L_Rect3.PtInRect(point))
  146.  {
  147.  m_FoundPoint=CPoint((m_SecondPoint.x+m_FirstPoint.x)/2,(m_SecondPoint.y+m_FirstPoint.y)/2);
  148.  return 3;
  149.  }
  150.  
  151.         return 0;
  152. }
  153. void CDLine::Circumrotate(CPoint first,CPoint second)
  154. {
  155. double k;
  156. int x;
  157. if(m_FirstPoint.x-m_SecondPoint.x==0)
  158.       k=99999999999;
  159. else  k=double(first.y-second.y)/(first.x-second.x);
  160.    x=(k*(first.y+second.y)+(first.x+second.x)+(k*k-1)*m_FirstPoint.x-2*k*m_FirstPoint.y)/(k*k+1);
  161.    m_FirstPoint.y=k*(x-this->m_FirstPoint.x)+this->m_FirstPoint.y;
  162.    m_FirstPoint.x=x;
  163.    x=(k*(first.y+second.y)+(first.x+second.x)+(k*k-1)*m_SecondPoint.x-2*k*m_SecondPoint.y)/(k*k+1);
  164.    m_SecondPoint.y=k*(x-this->m_SecondPoint.x)+this->m_SecondPoint.y;
  165.    m_SecondPoint.x=x;
  166.  }
  167. void CDLine::ShowSelectPoint(CDC*pDC)
  168. {
  169.  
  170.  CBrush brush;
  171. brush.CreateSolidBrush(RGB(0,0,255));
  172. CPen m_pen;
  173. m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  174. CPen *OldPen=pDC->SelectObject(&m_pen);
  175. int oldBkMode=pDC->SetBkMode(OPAQUE); 
  176. CBrush *OldBrush=pDC->SelectObject(&brush);
  177. int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
  178.  CRect rect(m_FirstPoint,m_FirstPoint);
  179.  rect.InflateRect(3,3);
  180.  pDC->Rectangle(rect);
  181.  
  182.  rect=CRect(m_SecondPoint,m_SecondPoint);
  183.  rect.InflateRect(3,3);
  184.  pDC->Rectangle(rect);
  185.  pDC->SelectObject(OldBrush);
  186.  brush.DeleteObject();
  187.  brush.CreateSolidBrush(RGB(255,0,0));
  188.  OldBrush=pDC->SelectObject(&brush);
  189.  rect=CRect((m_FirstPoint.x+m_SecondPoint.x)/2,(m_FirstPoint.y+m_SecondPoint.y)/2,(m_FirstPoint.x+m_SecondPoint.x)/2,(m_FirstPoint.y+m_SecondPoint.y)/2);
  190.  rect.InflateRect(3,3);
  191.  pDC->Rectangle(rect);
  192.  
  193. pDC->SelectObject(OldPen);
  194. pDC->SetBkMode(oldBkMode); 
  195. pDC->SelectObject(OldBrush);
  196. pDC->SetROP2(oldDrawingMode);
  197. }
  198. BOOL CDLine::IsInRgn(CPoint point)
  199. {
  200.    this->GetRgn();
  201.    return(m_Rgn->PtInRegion(point));
  202. }
  203. void CDLine::DrawActive(CDC *pDC,CPoint point)
  204. {
  205.  CPen m_pen;
  206.  m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  207.  CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  208.  COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  209.  int OldBkMode=pDC->SetBkMode(m_BkMode);
  210.  pDC->MoveTo(m_FirstPoint);
  211.  pDC->LineTo(m_SecondPoint);
  212.  pDC->SelectObject(pen);
  213.  pDC->SetBkMode(m_BkMode);
  214.  pDC->SetBkColor(OldColor);
  215. }
  216. void CDLine::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
  217. {
  218.       if(m_HaveFindFirst)
  219.    {
  220.    pView->L_iPointCount=IsOnMarginPoint(point);
  221.     if(pView->L_iPointCount==1)
  222.  { 
  223.    pView->m_bHaveFindSecond=true;
  224.    pView->m_CurrentDrawTool=DLine_Tool;
  225.    pView->m_CurrentDrawStatus=Change_Status;
  226.    pView->m_FirstPoint=m_SecondPoint;
  227.    pView->m_SecondPoint=point;
  228.  }
  229.          if(pView->L_iPointCount==2)
  230.  {   
  231. pView->m_bHaveFindSecond=true;
  232. pView->m_CurrentDrawTool=DLine_Tool;
  233.     pView->m_CurrentDrawStatus=Change_Status;
  234. pView->m_FirstPoint=m_FirstPoint;
  235. pView->m_SecondPoint=point;
  236.  }
  237.  if(pView->L_iPointCount==3)
  238.  {
  239.     pView->m_bHaveFindSecond=true;
  240.             pView->m_CurrentDrawTool=DLine_Tool;
  241. pView->m_CurrentDrawStatus=Drag_Status;
  242. pView->m_FirstPoint=m_FirstPoint;
  243. pView->m_SecondPoint=m_SecondPoint;
  244.  }
  245.    }//**********if(L_pDLine->m_HaveFindFirst)
  246.  if((!pView->m_bHaveFindSecond)&&IsInRgn(point))
  247.  if(!m_HaveFindFirst)
  248.  {
  249.        m_HaveFindFirst=true;
  250.                pView->m_bHaveFindFirst=true;
  251.    ShowSelectPoint(pDC);
  252.  }
  253.  else
  254.  {
  255.   this->m_HaveFindFirst=false;
  256.   pView->m_bHaveFindFirst=true;
  257.   this->ShowSelectPoint(pDC);
  258.  }
  259.  }
  260.         if(pView->m_bHaveFindSecond)
  261. {   
  262.         pView->m_pCurrentUnit=this;
  263.     m_DrawingMode=pDC->GetROP2();
  264. ShowSelectPoint(pDC);
  265.     DrawStatic(pDC);
  266. DrawOldReferencePoint(pDC,m_FoundPoint);
  267. }
  268. }
  269. void CDLine::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
  270. {
  271.     if(pView->m_CurrentDrawStatus==Drag_Status)
  272. {
  273. m_FirstPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  274.     m_SecondPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  275. }
  276. else
  277. {
  278.   pDC->SetROP2(R2_NOTXORPEN);
  279.   //m_DrawingMode=pDC->GetROP2();
  280.   this->DrawActive(pDC,point);
  281.       pView->m_SecondPoint=point;
  282.   //m_pCurrentUnit->m_FirstPoint=m_FirstPoint;
  283.   m_SecondPoint=pView->m_SecondPoint;
  284.       this->DrawActive(pDC,point);
  285. }
  286. }