drawView.cpp
上传用户:jinlangri
上传日期:2022-07-17
资源大小:10774k
文件大小:14k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1. // drawView.cpp : implementation of the CDrawView class
  2. //
  3. #include "stdafx.h"
  4. #include "draw.h"
  5. #include "TsFont.h"
  6. #include "word.h"
  7. #include "drawDoc.h"
  8. #include "drawView.h"
  9. #include<math.h>
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CDrawView
  17. IMPLEMENT_DYNCREATE(CDrawView, CScrollView)
  18. BEGIN_MESSAGE_MAP(CDrawView, CScrollView)
  19. //{{AFX_MSG_MAP(CDrawView)
  20. ON_WM_LBUTTONDOWN()
  21. ON_WM_MOUSEMOVE()
  22. ON_WM_LBUTTONUP()
  23. ON_WM_RBUTTONUP()
  24. ON_COMMAND(ID_DRAW_FONT, OnDrawFont)
  25. //}}AFX_MSG_MAP
  26. // Standard printing commands
  27. ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
  29. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CDrawView construction/destruction
  33. CDrawView::CDrawView()
  34. {
  35. // TODO: add construction code here
  36. m_Dragging = 0;
  37. m_IsRect = FALSE;
  38. m_hArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  39. m_hCross = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
  40. m_PenDotted.CreatePen(PS_DOT, 1, RGB(0,0,0));
  41. m_cTextColor = RGB(0,0,0);
  42. m_lf.lfHeight = 30; 
  43. m_lf.lfWidth = 0; 
  44. m_lf.lfEscapement = 0; 
  45. m_lf.lfOrientation = 0; 
  46. m_lf.lfWeight = FW_NORMAL; 
  47. m_lf.lfItalic = FALSE; 
  48. m_lf.lfUnderline = FALSE; 
  49. m_lf.lfStrikeOut = FALSE; 
  50. m_lf.lfCharSet = GB2312_CHARSET; 
  51. m_lf.lfOutPrecision = OUT_STROKE_PRECIS; 
  52. m_lf.lfClipPrecision = CLIP_STROKE_PRECIS; 
  53. m_lf.lfQuality = DRAFT_QUALITY; 
  54. m_lf.lfPitchAndFamily = VARIABLE_PITCH|FF_MODERN; 
  55. strcpy(m_lf.lfFaceName, "仿宋_GB2312"); 
  56. }
  57. CDrawView::~CDrawView()
  58. {
  59. }
  60. BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
  61. {
  62. // TODO: Modify the Window class or styles here by modifying
  63. //  the CREATESTRUCT cs
  64. m_ClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, 0,
  65. (HBRUSH)::GetStockObject(WHITE_BRUSH), 0);
  66. cs.lpszClass = m_ClassName;
  67. return CScrollView::PreCreateWindow(cs);
  68. }
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CDrawView drawing
  71. void CDrawView::OnDraw(CDC* pDC)
  72. {
  73. CDrawDoc* pDoc = GetDocument();
  74. ASSERT_VALID(pDoc);
  75. // TODO: add draw code for native data here
  76. CSize ScrollSize = GetTotalSize();
  77. pDC->MoveTo(ScrollSize.cx, 0);
  78. pDC->LineTo(ScrollSize.cx, ScrollSize.cy);
  79. pDC->LineTo(0, ScrollSize.cy);
  80. CRect ClipRect;
  81. CRect DimRect;
  82. CRect IntRect;
  83. CDrawBase *pDraw;
  84. pDC->GetClipBox(&ClipRect);
  85. int NumDraws = pDoc->GetNumDraws();
  86. for(int Index = 0; Index < NumDraws; ++Index)
  87. {
  88. pDraw = pDoc->GetDraw(Index);
  89. DimRect = pDraw->GetDimRect();
  90. if(IntRect.IntersectRect(DimRect, ClipRect))
  91. pDraw->Draw(pDC);
  92. }
  93. }
  94. void CDrawView::OnInitialUpdate()
  95. {
  96. CScrollView::OnInitialUpdate();
  97. CSize sizeTotal;
  98. sizeTotal.cx = 800;
  99. sizeTotal.cy = 600;
  100. SetScrollSizes(MM_TEXT, sizeTotal);
  101. }
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CDrawView printing
  104. BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
  105. {
  106. // default preparation
  107. return DoPreparePrinting(pInfo);
  108. }
  109. void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  110. {
  111. // TODO: add extra initialization before printing
  112. }
  113. void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  114. {
  115. // TODO: add cleanup after printing
  116. }
  117. /////////////////////////////////////////////////////////////////////////////
  118. // CDrawView diagnostics
  119. #ifdef _DEBUG
  120. void CDrawView::AssertValid() const
  121. {
  122. CScrollView::AssertValid();
  123. }
  124. void CDrawView::Dump(CDumpContext& dc) const
  125. {
  126. CScrollView::Dump(dc);
  127. }
  128. CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
  129. {
  130. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
  131. return (CDrawDoc*)m_pDocument;
  132. }
  133. #endif //_DEBUG
  134. /////////////////////////////////////////////////////////////////////////////
  135. // CDrawView message handlers
  136. void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
  137. {
  138. CClientDC ClientDC(this);
  139. OnPrepareDC(&ClientDC);
  140. CDrawApp *pApp = (CDrawApp *)AfxGetApp();
  141. if(m_IsRect)
  142. {
  143. CDC *pDC = GetDC();
  144. CRect rect(m_PointOrigin.x, m_PointOrigin.y, m_PointOld.x, m_PointOld.y);
  145. rect.NormalizeRect();
  146. pDC->DrawFocusRect(rect);
  147. ReleaseDC(pDC);
  148. m_IsRect = FALSE;
  149. }
  150. if(pApp->m_CurrentTool != ID_DRAW_SELECT && pApp->m_CurrentTool != ID_DRAW_WORD)
  151. ClientDC.DPtoLP(&point);
  152. CSize ScrollSize = GetTotalSize();
  153. CRect ScrollRect(0, 0, ScrollSize.cx, ScrollSize.cy);
  154. if(!ScrollRect.PtInRect(point))
  155. return;
  156. m_PointOrigin = point;
  157. m_PointOld = point;
  158. SetCapture();
  159. m_Dragging = 1;
  160. ClientDC.LPtoDP(&ScrollRect);
  161. CRect ViewRect;
  162. GetClientRect(&ViewRect);
  163. CRect IntRect;
  164. IntRect.IntersectRect(&ScrollRect, &ViewRect);
  165. ClientToScreen(&IntRect);
  166. ::ClipCursor(&IntRect);
  167. CScrollView::OnLButtonDown(nFlags, point);
  168. }
  169. void CDrawView::OnMouseMove(UINT nFlags, CPoint point) 
  170. {
  171. CClientDC ClientDC(this);
  172. OnPrepareDC(&ClientDC);
  173. CDrawApp *pApp = (CDrawApp *)AfxGetApp();
  174. if(pApp->m_CurrentTool != ID_DRAW_SELECT && pApp->m_CurrentTool != ID_DRAW_WORD)
  175. ClientDC.DPtoLP(&point);
  176. if(!m_Dragging)
  177. {
  178. CSize ScrollSize = GetTotalSize();
  179. CRect ScrollRect(0,0,ScrollSize.cx, ScrollSize.cy);
  180. if(ScrollRect.PtInRect(point))
  181. ::SetCursor(m_hCross);
  182. else
  183. ::SetCursor(m_hArrow);
  184. return;
  185. }
  186. ClientDC.SetROP2(R2_NOT);
  187. ClientDC.SelectObject(&m_PenDotted);
  188. ClientDC.SetBkColor(TRANSPARENT);
  189. ClientDC.SelectStockObject(NULL_BRUSH);
  190. switch(((CDrawApp *)AfxGetApp())->m_CurrentTool)
  191. {
  192. case ID_DRAW_LINE:
  193. ClientDC.MoveTo(m_PointOrigin);
  194. ClientDC.LineTo(m_PointOld);
  195. ClientDC.MoveTo(m_PointOrigin);
  196. ClientDC.LineTo(point);
  197. break;
  198. case ID_DRAW_SELECT:
  199. case ID_DRAW_WORD:
  200. {
  201. CDC *pDC = GetDC();
  202. CRect rect(m_PointOrigin.x, m_PointOrigin.y, m_PointOld.x, m_PointOld.y);
  203. rect.NormalizeRect();
  204. pDC->DrawFocusRect(rect);
  205. rect.SetRect(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y);
  206. rect.NormalizeRect();
  207. pDC->DrawFocusRect(rect);
  208. m_IsRect = TRUE;
  209. ReleaseDC(pDC);
  210. break;
  211. }
  212. case ID_DRAW_RECT:
  213. case ID_DRAW_RECTFILL:
  214. ClientDC.Rectangle(m_PointOrigin.x, m_PointOrigin.y,
  215. m_PointOld.x, m_PointOld.y);
  216. ClientDC.Rectangle(m_PointOrigin.x, m_PointOrigin.y,
  217. point.x, point.y);
  218. break;
  219. case ID_DRAW_RECTROUND:
  220. case ID_DRAW_RECTROUNDFILL:
  221. {
  222. int SizeRound = (abs(m_PointOld.x - m_PointOrigin.x) +
  223. abs(m_PointOld.y - m_PointOrigin.x))/6;
  224. ClientDC.RoundRect(m_PointOrigin.x, m_PointOrigin.y,
  225. m_PointOld.x, m_PointOld.y,
  226. SizeRound, SizeRound);
  227.  SizeRound = (abs(point.x - m_PointOrigin.x) +
  228. abs(point.y - m_PointOrigin.x))/6;
  229. ClientDC.RoundRect(m_PointOrigin.x, m_PointOrigin.y,
  230. point.x, point.y, SizeRound, SizeRound);
  231. break;
  232. }
  233. case ID_DRAW_CIRCLE:
  234. case ID_DRAW_CIRCLEFILL:
  235. {
  236. int dim = int(sqrt((m_PointOld.x - m_PointOrigin.x) * (m_PointOld.x - m_PointOrigin.x)
  237.   + (m_PointOld.y - m_PointOrigin.y) * (m_PointOld.y - m_PointOrigin.y)));
  238. ClientDC.Ellipse(m_PointOrigin.x - dim,m_PointOrigin.y - dim,
  239. m_PointOrigin.x + dim, m_PointOrigin.y + dim);
  240. dim = int(sqrt((point.x - m_PointOrigin.x) * (point.x - m_PointOrigin.x)
  241.   + (point.y - m_PointOrigin.y) * (point.y - m_PointOrigin.y)));
  242. ClientDC.Ellipse(m_PointOrigin.x - dim,m_PointOrigin.y - dim,
  243. m_PointOrigin.x + dim, m_PointOrigin.y + dim);
  244. break;
  245. }
  246. case ID_DRAW_ELLIPSE:
  247. case ID_DRAW_ELLIPSEFILL:
  248. ClientDC.Ellipse(m_PointOrigin.x, m_PointOrigin.y,
  249. m_PointOld.x, m_PointOld.y);
  250. ClientDC.Ellipse(m_PointOrigin.x, m_PointOrigin.y,
  251. point.x, point.y);
  252. break;
  253. }
  254. m_PointOld = point;
  255. CScrollView::OnMouseMove(nFlags, point);
  256. }
  257. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
  258. {
  259. if(!m_Dragging) return;
  260. m_Dragging = 0;
  261. ::ReleaseCapture();
  262. ::ClipCursor(NULL);
  263. CClientDC ClientDC(this);
  264. OnPrepareDC(&ClientDC);
  265. ClientDC.DPtoLP(&point);
  266. ClientDC.SelectObject(&m_PenDotted);
  267. ClientDC.SetROP2(R2_NOT);
  268. ClientDC.SetBkMode(TRANSPARENT);
  269. ClientDC.SelectStockObject(NULL_BRUSH);
  270. CDrawApp *pApp = (CDrawApp *)AfxGetApp();
  271. CDrawBase *pDraw;
  272. BOOL flag = FALSE;
  273. switch(pApp->m_CurrentTool)
  274. {
  275. case ID_DRAW_LINE:
  276. ClientDC.MoveTo(m_PointOrigin);
  277. ClientDC.LineTo(m_PointOld);
  278. pDraw = new CLine(m_PointOrigin.x, m_PointOrigin.y,
  279. point.x, point.y, pApp->m_PenColor, pApp->m_CurrentPenThickness);
  280. break;
  281. case ID_DRAW_RECT:
  282. ClientDC.Rectangle(m_PointOrigin.x, m_PointOrigin.y,
  283. m_PointOld.x, m_PointOld.y);
  284. pDraw = new CRectangle(m_PointOrigin.x, m_PointOrigin.y,point.x, point.y,
  285. pApp->m_PenColor, pApp->m_CurrentPenThickness);
  286. break;
  287. case ID_DRAW_RECTFILL:
  288. ClientDC.Rectangle(m_PointOrigin.x, m_PointOrigin.y,
  289. m_PointOld.x, m_PointOld.y);
  290. pDraw = new CRectFill(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
  291. pApp->m_FillColor);
  292. break;
  293. case ID_DRAW_RECTROUND:
  294. {
  295. int SizeRound = (abs(m_PointOld.x - m_PointOrigin.x) +
  296. abs(m_PointOld.y - m_PointOrigin.x))/6;
  297. ClientDC.RoundRect(m_PointOrigin.x, m_PointOrigin.y,
  298. m_PointOld.x, m_PointOld.y,
  299. SizeRound, SizeRound);
  300. pDraw = new CRectRound(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
  301. pApp->m_PenColor, pApp->m_CurrentPenThickness);
  302. break;
  303. }
  304. case ID_DRAW_RECTROUNDFILL:
  305. { int SizeRound = (abs(m_PointOld.x - m_PointOrigin.x) +
  306. abs(m_PointOld.y-m_PointOrigin.x))/6;
  307. ClientDC.RoundRect(m_PointOrigin.x, m_PointOrigin.y,
  308. m_PointOld.x, m_PointOld.y,
  309. SizeRound, SizeRound);
  310. pDraw = new CRectRoundFill(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
  311. pApp->m_FillColor);
  312. break;
  313. }
  314. case ID_DRAW_CIRCLE:
  315. {
  316. int dim = int(sqrt((m_PointOld.x - m_PointOrigin.x) * (m_PointOld.x - m_PointOrigin.x)
  317.   + (m_PointOld.y - m_PointOrigin.y) * (m_PointOld.y - m_PointOrigin.y)));
  318. ClientDC.Ellipse(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
  319. m_PointOrigin.x + dim, m_PointOrigin.y + dim);
  320. dim = int(sqrt((point.x - m_PointOrigin.x) * (point.x - m_PointOrigin.x)
  321.   + (point.y - m_PointOrigin.y) * (point.y - m_PointOrigin.y)));
  322. ClientDC.Ellipse(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
  323. m_PointOrigin.x + dim, m_PointOrigin.y + dim);
  324. pDraw = new CCircle(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
  325. m_PointOrigin.x + dim, m_PointOrigin.y + dim,
  326. pApp->m_PenColor, pApp->m_CurrentPenThickness);
  327. break;
  328. }
  329. case ID_DRAW_CIRCLEFILL:
  330. {
  331. ClientDC.MoveTo(m_PointOrigin);
  332. ClientDC.LineTo(m_PointOld);
  333. int dim = int(sqrt((m_PointOld.x - m_PointOrigin.x) * (m_PointOld.x - m_PointOrigin.x)
  334.   + (m_PointOld.y - m_PointOrigin.y) * (m_PointOld.y - m_PointOrigin.y)));
  335. ClientDC.Ellipse(m_PointOrigin.x - dim,m_PointOrigin.y - dim,
  336. m_PointOrigin.x + dim, m_PointOrigin.y + dim);
  337. dim = int(sqrt((point.x - m_PointOrigin.x) * (point.x - m_PointOrigin.x)
  338.   + (point.y - m_PointOrigin.y) * (point.y - m_PointOrigin.y)));
  339. ClientDC.Ellipse(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
  340. m_PointOrigin.x + dim, m_PointOrigin.y + dim);
  341. pDraw = new CCircleFill(m_PointOrigin.x - dim, m_PointOrigin.y - dim,
  342. m_PointOrigin.x + dim, m_PointOrigin.y + dim,
  343. pApp->m_FillColor);
  344. break;
  345. }
  346. case ID_DRAW_ELLIPSE:
  347. {
  348. ClientDC.Ellipse(m_PointOrigin.x, m_PointOrigin.y,
  349. m_PointOld.x, m_PointOld.y);
  350. pDraw = new CCircle(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
  351. pApp->m_PenColor, pApp->m_CurrentPenThickness);
  352. break;
  353. }
  354. case ID_DRAW_ELLIPSEFILL:
  355. ClientDC.Ellipse(m_PointOrigin.x, m_PointOrigin.y,
  356. m_PointOld.x, m_PointOld.y);
  357. pDraw = new CCircleFill(m_PointOrigin.x, m_PointOrigin.y, point.x, point.y,
  358. pApp->m_FillColor);
  359. break;
  360. case ID_DRAW_WORD:
  361. {
  362. CPoint PointOrigin = m_PointOrigin;
  363. ClientDC.DPtoLP(&PointOrigin);
  364. CString str;
  365. CWord WordDlg;
  366. if(WordDlg.DoModal() == IDOK)
  367. {
  368. str = WordDlg.m_Word;
  369. pDraw = new CText(PointOrigin.x, PointOrigin.y, point.x, point.y, m_cTextColor, m_lf, str);
  370. flag = TRUE;
  371. }
  372. if(m_IsRect)
  373. {
  374. CRect rect(m_PointOrigin.x, m_PointOrigin.y, m_PointOld.x, m_PointOld.y);
  375. rect.NormalizeRect();
  376. ClientDC.DrawFocusRect(rect);
  377. m_IsRect = FALSE;
  378. }
  379. }
  380. }
  381. if(pApp->m_CurrentTool != ID_DRAW_SELECT && pApp->m_CurrentTool != ID_DRAW_WORD)
  382. {
  383. ClientDC.SetROP2(R2_COPYPEN);
  384. pDraw->Draw(&ClientDC);
  385. CDrawDoc *pDoc = GetDocument();
  386. pDoc->AddDraw(pDraw);
  387. pDoc->UpdateAllViews(this, 0, pDraw);
  388. }
  389. else if(pApp->m_CurrentTool == ID_DRAW_WORD&&flag)
  390. {
  391. ClientDC.SetROP2(R2_COPYPEN);
  392. pDraw->Draw(&ClientDC);
  393. CDrawDoc *pDoc = GetDocument();
  394. pDoc->AddDraw(pDraw);
  395. pDoc->UpdateAllViews(this, 0, pDraw);
  396. }
  397. CScrollView::OnLButtonUp(nFlags, point);
  398. }
  399. void CDrawView::OnRButtonUp(UINT nFlags, CPoint point) 
  400. {
  401. // TODO: Add your message handler code here and/or call default
  402. if(m_IsRect)
  403. {
  404. CDC *pDC = GetDC();
  405. CRect rect(m_PointOrigin.x, m_PointOrigin.y, m_PointOld.x, m_PointOld.y);
  406. rect.NormalizeRect();
  407. pDC->DrawFocusRect(rect);
  408. ReleaseDC(pDC);
  409. m_IsRect = FALSE;
  410. }
  411. CScrollView::OnRButtonUp(nFlags, point);
  412. }
  413. void CDrawView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  414. {
  415.  if(pHint != 0)
  416.  {
  417.  CRect InvalidRect = ((CLine *)pHint)->GetDimRect();
  418.  CClientDC ClientDC(this);
  419.  OnPrepareDC(&ClientDC);
  420.  ClientDC.LPtoDP(&InvalidRect);
  421.  InvalidateRect(&InvalidRect);
  422.  }
  423.  else
  424.  CScrollView::OnUpdate(pSender,lHint,pHint);
  425. }
  426. void CDrawView::OnDrawFont() 
  427. {
  428. CTsFont TsFont;
  429. TsFont.m_lfHeight = m_lf.lfHeight;
  430. TsFont.m_lfWidth = m_lf.lfWidth;
  431. TsFont.m_lfEscapement = m_lf.lfEscapement;
  432. TsFont.m_lfOrientation = m_lf.lfOrientation;
  433. TsFont.m_lfWeight = m_lf.lfWeight;
  434. TsFont.m_crColor = m_cTextColor;
  435. if(TsFont.DoModal() == IDOK)
  436. {
  437.    m_lf.lfHeight = TsFont.m_lfHeight; 
  438.    m_lf.lfWidth = TsFont.m_lfWidth; 
  439.    m_lf.lfEscapement = TsFont.m_lfEscapement; 
  440.    m_lf.lfOrientation = TsFont.m_lfOrientation; 
  441.    m_lf.lfWeight = TsFont.m_lfWeight; 
  442.    m_cTextColor = TsFont.m_crColor;
  443.  }
  444. }