CText.cpp 10 KB

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