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

绘图程序

开发平台:

C/C++

  1. #include"stdafx.h"
  2. #include"CText.h"
  3. #include "EastDrawView.h"
  4. #include"TextEdit.h"
  5. IMPLEMENT_SERIAL(CText,CUnit,1)
  6. CText::CText()
  7. {
  8.  CUnit::Initial();
  9.  m_Font=new CFont;
  10.  m_Font->CreatePointFont (100,_T("Arial"));
  11.  m_pTextEdit = new CTextEdit;
  12.  m_HaveFinish=false;
  13. }
  14. CText::~CText()
  15. {
  16. delete m_Font;
  17. delete m_pTextEdit;
  18. }
  19. void CText::Serialize(CArchive &ar)
  20. {
  21. CUnit::Serialize(ar);
  22.   
  23.     LOGFONT m_lf; 
  24.   
  25.  
  26. if(ar.IsStoring())
  27.    {
  28.      m_Font->GetLogFont(&m_lf);
  29.     ar<<m_Hight<<m_Width<<m_PositionPoint<<m_String;
  30.     ar<<m_lf.lfCharSet;
  31. ar<<m_lf.lfClipPrecision;
  32. ar<<m_lf.lfEscapement;
  33. ar<<m_lf.lfHeight;
  34. ar<<m_lf.lfItalic;
  35. ar<<m_lf.lfOrientation;
  36. ar<<m_lf.lfOutPrecision;
  37. ar<<m_lf.lfPitchAndFamily;
  38. ar<<m_lf.lfQuality;
  39.     ar<<m_lf.lfWidth;
  40. ar<<m_lf.lfWeight;
  41. ar<<m_lf.lfUnderline;
  42. ar<<m_lf.lfStrikeOut;
  43.         CString str=m_lf.lfFaceName;
  44.         ar<<str;
  45.    }
  46. else
  47.    {
  48.        m_HaveFinish=true;
  49.        ar>>m_Hight>>m_Width>>m_PositionPoint>>m_String;
  50.     ar>>m_lf.lfCharSet;
  51. ar>>m_lf.lfClipPrecision;
  52. ar>>m_lf.lfEscapement;
  53. ar>>m_lf.lfHeight;
  54. ar>>m_lf.lfItalic;
  55. ar>>m_lf.lfOrientation;
  56. ar>>m_lf.lfOutPrecision;
  57. ar>>m_lf.lfPitchAndFamily;
  58. ar>>m_lf.lfQuality;
  59.     ar>>m_lf.lfWidth;
  60. ar>>m_lf.lfWeight;
  61. ar>>m_lf.lfUnderline;
  62. ar>>m_lf.lfStrikeOut;
  63. CString str;
  64. ar>>str;
  65. ::lstrcpy(m_lf.lfFaceName,str);
  66. m_Font->DeleteObject();
  67. m_Font->CreateFontIndirect(&m_lf);
  68. m_bHaveCreat=false;
  69.    }
  70. }
  71. void CText::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
  72. {
  73.  if(pView->m_CurrentDrawStatus==Draw_Status)
  74.  {
  75.    if(pView->m_LBDownTimes==1&&!m_pTextEdit->m_IsMoving)
  76.    {
  77. CPoint point1(10,10);
  78. CPoint point2(200,200);
  79. m_pTextEdit->Create(WS_VSCROLL|WS_HSCROLL|ES_MULTILINE | ES_WANTRETURN | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
  80.     CRect(point1,point2), pView, 1);
  81. this->SetTextEditPosition(pDC,pView,point);
  82. m_pTextEdit->ShowWindow(true);
  83. pView->ShowFontBar(true);
  84. pView->SetBoxFont();
  85.    }
  86.   if(pView->m_LBDownTimes==2&&!m_pTextEdit->m_IsMoving)
  87.   {
  88.    pView->ShowFontBar(false);
  89.    m_pTextEdit->UpdateData();
  90.    m_pTextEdit->ShowWindow(false);
  91.    m_HaveFinish=true; 
  92.    m_pTextEdit->GetWindowText(m_String);
  93.    pView->m_LBDownTimes=0;
  94.    pView->ExchangeSave();
  95.    
  96.    
  97.   if(this->m_DrawStatus==Change_Status)
  98.    {
  99.     this->m_DrawStatus=Draw_Status;
  100. pView->m_CurrentDrawStatus=Select_Status;
  101.    }
  102.    
  103.    int nLineCount = m_pTextEdit->GetLineCount();
  104.    CString strText;
  105.    CFont *pOldFond=pDC->SelectObject(m_Font);
  106.    int pOldBkMode=pDC->SetBkMode(this->m_BkMode);
  107.    COLORREF oldBKColor=pDC->SetBkColor(this->m_BackgroundColor);
  108.    COLORREF oldPenColor=pDC->SetTextColor(this->m_PenColor);
  109.    CBrush brush;
  110.    LOGBRUSH brushlog;
  111.    brushlog.lbColor=m_BrushColor;
  112.    brushlog.lbHatch=m_BrushHatch;
  113.    brushlog.lbStyle=m_BrushStyle;
  114.    brush.CreateBrushIndirect(&brushlog);
  115.    CBrush*pOldBrush=pDC->SelectObject(&brush);
  116.    TEXTMETRIC tm;
  117.    pDC->GetTextMetrics(&tm);
  118.    int maxLength=0;
  119.    int maxLineLong;
  120.    
  121.   for(int i=0;i<nLineCount;i++)
  122.   {
  123.    maxLength=maxLength>m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i))?maxLength:m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i));
  124.    m_pTextEdit->GetLine(0,strText.GetBuffer(m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i))),m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i)));
  125.    
  126.    strText.ReleaseBuffer();
  127.    pDC->TextOut(m_PositionPoint.x,m_PositionPoint.y+i*(tm.tmHeight+tm.tmExternalLeading),strText.Left(m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i))));
  128.    
  129.    strText.Empty();
  130.    
  131.   }
  132.   
  133.   this->m_Width=maxLength*(tm.tmMaxCharWidth);
  134.   this->m_Hight=(nLineCount+1)*(tm.tmHeight+tm.tmExternalLeading);
  135.   CRect rect;
  136.   m_pTextEdit->GetWindowRect(&rect);
  137.   m_Width=m_Width>rect.Width()?m_Width:rect.Width();
  138.   m_Hight=m_Hight>rect.Height()?m_Hight:rect.Height();
  139.    
  140.    pDC->SetBkMode(pOldBkMode);
  141.    pDC->SelectObject(pOldFond);
  142.    pDC->SetBkColor(oldBKColor);
  143.    pDC->SetTextColor(oldPenColor);
  144.    pDC->SelectObject(pOldBrush);
  145.   }
  146.  if(pView->m_LBDownTimes==2&&m_pTextEdit->m_IsMoving)
  147.  {
  148. pView->ShowFontBar(true);
  149. pView->m_LBDownTimes=1;
  150. pView->ExchangeSave();
  151.     
  152. m_PositionPoint=point;
  153. m_pTextEdit->m_PositionPoint=point;
  154. CRect rect;
  155.     m_pTextEdit->GetWindowRect(&rect);
  156.     pDC->DPtoLP(&rect);
  157. pDC->DrawFocusRect(CRect(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1));
  158. this->SetTextEditPosition(pDC,pView,m_PositionPoint);
  159. this->m_pTextEdit->m_IsMoving=false;
  160.  }
  161.  
  162. }
  163.  if(pView->m_CurrentDrawStatus==Change_Status)
  164.  {
  165.     pView->m_CurrentDrawStatus=Draw_Status;
  166. pView->m_LBDownTimes=1;
  167.  }//********if(pView->m_CurrentDrawStatus==Exchange_Status)
  168.  if(pView->m_CurrentDrawStatus==Select_Status)
  169.  {
  170.    if(IsOnMarginPoint(point))
  171.    {
  172.     if(m_HaveFindFirst)
  173.     {
  174.  pView->ShowFontBar(true);
  175.      this->m_HaveFindFirst=false;
  176.  pView->m_bHaveFindSecond=true;
  177.      this->ShowSelectPoint(pDC);
  178.  CPoint point=m_PositionPoint;
  179.  pDC->LPtoDP(&point);
  180.  
  181.       CRect rect;
  182.       m_pTextEdit->GetWindowRect(&rect);
  183.       m_Width=m_Width>rect.Width()?m_Width:rect.Width();
  184.       m_Hight=m_Hight>rect.Height()?m_Hight:rect.Height();
  185.  this->m_pTextEdit->MoveWindow(point.x,point.y,m_Width,m_Hight);
  186.      this->m_pTextEdit->ShowWindow(true);
  187.  this->m_pTextEdit->SetFont(m_Font);
  188.  pView->m_CurrentDrawTool=Text_Tool;
  189.  pView->m_CurrentDrawStatus=Change_Status;
  190.  pView->m_pCurrentUnit=this;
  191.  pView->m_LBDownTimes=0;
  192.  m_HaveFinish=false;
  193.  this->m_DrawStatus=Change_Status;
  194.  }
  195.    }
  196.     if(!pView->m_bHaveFindSecond&&IsInRgn(point))
  197.  
  198. {
  199.  if(!m_HaveFindFirst)
  200.  {
  201.  m_HaveFindFirst=true;
  202.      pView->m_bHaveFindFirst=true;
  203.      ShowSelectPoint(pDC);
  204.  }
  205. else
  206. {
  207.      m_HaveFindFirst=false;
  208.      pView->m_bHaveFindFirst=true;
  209.      ShowSelectPoint(pDC);
  210. }
  211. }
  212.   }//******if(pView->m_CurrentDrawStatus==Select_Status)
  213.  }
  214. void CText::SetTextEditPosition(CDC *pDC, CEastDrawView *pView, CPoint point)
  215. {
  216.   CRect rect;
  217.   m_pTextEdit->GetWindowRect(&rect);
  218.   
  219.  
  220.  
  221.   m_PositionPoint=point;
  222.   m_pTextEdit->m_PositionPoint=point;
  223.   this->m_Width=rect.Width();
  224.   this->m_Hight=rect.Height();
  225.   pDC->LPtoDP(&point);
  226.   m_pTextEdit->m_PositionPoint=point;
  227.   m_pTextEdit->MoveWindow(point.x,point.y,rect.Width(),rect.Height());
  228. }
  229. void CText::SetFont(ENUMLOGFONT *lpelf)
  230. {
  231.  m_pTextEdit->SetMyFont(m_Font);
  232. }
  233. void CText::ChangeFont(ENUMLOGFONT *lpelf)
  234. {
  235.  
  236.  m_Font->DeleteObject();
  237.  m_Font->CreateFontIndirect(&lpelf->elfLogFont);
  238.  SetFont(lpelf);
  239. }
  240. void CText::ChangeFontSize(ENUMLOGFONT *lpelf)
  241. {
  242.  m_Font->DeleteObject();
  243.  m_Font->CreateFontIndirect(&lpelf->elfLogFont);
  244.  SetFont(lpelf);
  245. }
  246. void CText::DrawStatic(CDC *pDC)
  247. {
  248.  if(this->m_HaveFinish)
  249.  {
  250.   CMDIChildWnd*pChild=((CMDIFrameWnd*)AfxGetMainWnd())->MDIGetActive();
  251.   CEastDrawView*pView=(CEastDrawView*)pChild->GetActiveView();
  252.   if(!m_bHaveCreat)
  253.   {
  254.    m_pTextEdit->DestroyWindow();
  255.    m_pTextEdit->Create(WS_VSCROLL|WS_HSCROLL|ES_MULTILINE | ES_WANTRETURN | WS_CHILD |WS_TABSTOP | WS_BORDER,
  256.    CRect(m_PositionPoint,CPoint(m_PositionPoint.x+m_Width,m_PositionPoint.y+m_Hight)), pView, 1);
  257.    m_bHaveCreat=true;
  258.   }
  259.  m_pTextEdit->SetWindowText(m_String);
  260.  int nLineCount = m_pTextEdit->GetLineCount();
  261.  CString strText;
  262.  CFont *pOldFond=pDC->SelectObject(m_Font);
  263.  int pOldBkMode=pDC->SetBkMode(this->m_BkMode);
  264.  COLORREF oldBKColor=pDC->SetBkColor(this->m_BackgroundColor);
  265.  COLORREF oldPenColor=pDC->SetTextColor(this->m_PenColor);
  266.  CBrush brush;
  267.  LOGBRUSH brushlog;
  268.  brushlog.lbColor=m_BrushColor;
  269.  brushlog.lbHatch=m_BrushHatch;
  270.  brushlog.lbStyle=m_BrushStyle;
  271.  brush.CreateBrushIndirect(&brushlog);
  272.  CBrush*pOldBrush=pDC->SelectObject(&brush);
  273.  TEXTMETRIC tm;
  274.  pDC->GetTextMetrics(&tm);
  275.  
  276.  for(int i=0;i<nLineCount;i++)
  277.   {
  278.   m_pTextEdit->GetLine(i,strText.GetBuffer(m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i))),m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i)));
  279.   strText.ReleaseBuffer();
  280.   pDC->TextOut(m_PositionPoint.x,m_PositionPoint.y+i*(tm.tmHeight+tm.tmExternalLeading),strText.Left(m_pTextEdit->LineLength(m_pTextEdit->LineIndex(i))));
  281.   strText.Empty();
  282.   }
  283.   pDC->SetBkMode(pOldBkMode);
  284.   pDC->SelectObject(pOldFond);
  285.   pDC->SetBkColor(oldBKColor);
  286.   pDC->SetTextColor(oldPenColor);
  287.   pDC->SelectObject(pOldBrush);
  288.  }
  289.  
  290. }
  291. void CText::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
  292. {
  293.   CRect rect;
  294.   m_pTextEdit->GetWindowRect(&rect);
  295.   pDC->DPtoLP(&rect);
  296.   CPen pen;
  297.   pen.CreatePen(PS_SOLID,2,RGB(255,0,0));
  298.   CPen*pOldPen=pDC->SelectObject(&pen);
  299.   if(m_pTextEdit->m_IsMoving)
  300.   {
  301.     //this->SetTextEditPosition(pDC,pView,point);
  302.     //pDC->Rectangle(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1);
  303.     pDC->DrawFocusRect(CRect(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1));
  304.   m_PositionPoint=point;
  305. // pDC->Rectangle(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1);
  306.    pDC->DrawFocusRect(CRect(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1));
  307.   }
  308.   pDC->SelectObject(pOldPen);
  309. }
  310. void CText::OnMenuitemOk(CDC *pDC, CEastDrawView *pView)
  311. {
  312.   if(m_pTextEdit->m_IsMoving)
  313.   {
  314.     CRect rect;
  315.     m_pTextEdit->GetWindowRect(&rect);
  316.     pDC->DPtoLP(&rect);
  317. pDC->DrawFocusRect(CRect(m_PositionPoint.x+1,m_PositionPoint.y+1,m_PositionPoint.x+rect.Width()-1,m_PositionPoint.y+rect.Height()-1));
  318. SetTextEditPosition(pDC,pView,m_PositionPoint);
  319. m_pTextEdit->m_IsMoving=false;
  320.   }
  321. }
  322. BOOL CText::IsInRgn(CPoint point)
  323. {
  324. CRect rect1;
  325. CRect rect2;
  326. rect1.SetRect(m_PositionPoint.x-5,m_PositionPoint.y-5,m_PositionPoint.x+m_Width+5,m_PositionPoint.y+m_Hight+5);
  327. rect2.SetRect(m_PositionPoint.x+10,m_PositionPoint.y+10,m_PositionPoint.x+m_Width-10,m_PositionPoint.y+m_Hight-10);
  328. return rect1.PtInRect(point)&&!rect2.PtInRect(point);
  329. }
  330. int CText::IsOnMarginPoint(CPoint point)
  331. {
  332. CRect rect1;
  333. CRect rect2;
  334. rect1.SetRect(m_PositionPoint.x-3,m_PositionPoint.y-3,m_PositionPoint.x+m_Width+3,m_PositionPoint.y+m_Hight+3);
  335. rect2.SetRect(m_PositionPoint.x+3,m_PositionPoint.y+3,m_PositionPoint.x+m_Width-3,m_PositionPoint.y+m_Hight-3);
  336. if(rect1.PtInRect(point)&&!rect2.PtInRect(point))
  337. return 1;
  338. return 0;
  339. }
  340. void CText::ShowSelectPoint(CDC *pDC)
  341. {
  342. CPen m_pen;
  343. m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  344. CPen *OldPen=pDC->SelectObject(&m_pen);
  345. int oldBkMode=pDC->SetBkMode(TRANSPARENT);
  346. int OldRop=pDC->SetROP2(R2_NOTXORPEN);   
  347. CRect rect;
  348. rect.SetRect(m_PositionPoint.x,m_PositionPoint.y,m_PositionPoint.x+m_Width,m_PositionPoint.y+m_Hight);
  349. pDC->Rectangle(rect);
  350. pDC->SelectObject(OldPen);
  351. pDC->SetBkMode(oldBkMode);
  352. pDC->SetROP2(OldRop);
  353. }