Graph.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // CGraph.cpp: implementation of the CGraph class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. //#include <afxtempl.h>
  6. #include "drawobj.h"
  7. #include "graph.h"
  8. //---------------------------------------------- CGraph ---------
  9. CGraph::CGraph(CSize size)
  10. {
  11. m_Type = 1;
  12. m_sizePic.cx = size.cx;
  13. m_sizePic.cy = size.cy;
  14. m_bGrid = FALSE;
  15. m_sizeGrid.cx = 40;
  16. m_sizeGrid.cy = 40;
  17. m_clrBack = RGB(230, 241, 249);//RGB(255, 255, 255);//RGB(92, 92, 92);画面默认背景 颜色
  18. }
  19. CGraph::~CGraph(void)
  20. {
  21. #if GRAPH_DEBUG
  22. if(!m_DrawObjList.IsEmpty())
  23. {
  24. for(POSITION pos = m_DrawObjList.GetHeadPosition();pos!=NULL;)
  25. {
  26. POSITION pos1 = pos ; //要在这里做一个备份
  27. CDrawObj *ct = (CDrawObj *)m_DrawObjList.GetNext(pos);//他会改变 pos的值
  28. m_DrawObjList.RemoveAt(pos1);
  29. delete ct;
  30. }
  31. m_DrawObjList.RemoveAll();
  32. ASSERT(m_DrawObjList.IsEmpty());
  33. }
  34. #else
  35. POSITION pos = m_DrawObjList.GetHeadPosition();
  36. while(pos!=NULL) delete m_DrawObjList.GetNext(pos);
  37. m_DrawObjList.RemoveAll();
  38. #endif
  39. }
  40. void CGraph::SetPicFile(CString strPicFile)
  41. {
  42. m_strPicFile = strPicFile;
  43. }
  44. void CGraph::SetView(CView* pView)
  45. {
  46. m_pView = pView;
  47. #if GRAPH_DEBUG
  48. if(!m_DrawObjList.IsEmpty())
  49. {
  50. for(POSITION pos = m_DrawObjList.GetHeadPosition();pos!=NULL;)
  51. {
  52. POSITION pos1 = pos ; //要在这里做一个备份
  53. CDrawObj* pObj = m_DrawObjList.GetAt(pos1);
  54. pObj->m_pView = m_pView;
  55. m_DrawObjList.GetNext(pos);//他会改变 pos的值
  56. }
  57. }
  58. #else
  59. POSITION pos = m_DrawObjList.GetHeadPosition();
  60. while(pos!=NULL)
  61. {
  62. CDrawObj* pObj = m_DrawObjList.GetNext(pos);
  63. pObj->m_pView = m_pView;
  64. }
  65. #endif
  66. }
  67. void CGraph::Load(void)
  68. {
  69. #if GRAPH_DEBUG
  70. CView* _pView=NULL;
  71. if(!m_DrawObjList.IsEmpty())
  72. {
  73. for(POSITION pos = m_DrawObjList.GetHeadPosition();pos!=NULL;)
  74. {
  75. POSITION pos1 = pos ; //要在这里做一个备份
  76. CDrawObj *ct = (CDrawObj *)m_DrawObjList.GetNext(pos);//他会改变 pos的值
  77. _pView = ct->m_pView;
  78. m_DrawObjList.RemoveAt(pos1);
  79. delete ct;
  80. }
  81. m_DrawObjList.RemoveAll();
  82. ASSERT(m_DrawObjList.IsEmpty());
  83. }
  84. #else
  85. POSITION pos = m_DrawObjList.GetHeadPosition();
  86. while(pos!=NULL) delete m_DrawObjList.GetNext(pos);
  87. m_DrawObjList.RemoveAll();
  88. #endif
  89. CFile file;
  90. if(file.Open(m_strPicFile,CFile::modeRead))
  91. {
  92. CArchive ar(&file,CArchive::load);
  93. ar >> m_Type;
  94. ar >> m_sizePic;
  95. ar >> m_sizeGrid;
  96. ar >> m_bGrid;
  97. ar >> m_clrBack;
  98. m_DrawObjList.Serialize(ar);
  99. //编辑按钮控件数次,有撤销显示时,继续编辑,点击撤销,客户端会崩溃 解决
  100. if(!m_DrawObjList.IsEmpty() && _pView )
  101. {
  102. for(POSITION pos = m_DrawObjList.GetHeadPosition();pos!=NULL;)
  103. {
  104. POSITION pos1 = pos ;
  105. CDrawObj *ct = (CDrawObj *)m_DrawObjList.GetNext(pos);ct->m_strCaption;
  106. ct->m_pView = _pView;
  107. //delete ct;
  108. }
  109. }
  110. ///////////////////////////////////////
  111. ar.Close();
  112. file.Close();
  113. }
  114. }
  115. void CGraph::Store(void)
  116. {
  117. CFile file;
  118. if(file.Open(m_strPicFile,CFile::modeCreate|CFile::modeWrite))
  119. {
  120. CArchive ar(&file,CArchive::store);
  121. ar << m_Type;
  122. ar << m_sizePic;
  123. ar << m_sizeGrid;
  124. ar << m_bGrid;
  125. ar << m_clrBack;
  126. m_DrawObjList.Serialize(ar);
  127. ar.Flush();
  128. ar.Close();
  129. file.Flush();
  130. file.Close();
  131. }
  132. }
  133. CRect CGraph::Draw(CDC* pDC,CString &sOCXPath,int &iOcxIndex,BOOL bPrint)
  134. {
  135. CRect rectRet=NULL;
  136. CRect rect;
  137. if ( !bPrint )
  138. {
  139. if (g_bRun)
  140. {
  141. m_pView->GetClientRect(rect);
  142. CRect tmp = CRect(CPoint(0,0),m_sizePic);;
  143. rect.UnionRect( rect, tmp );
  144. pDC->FillSolidRect(rect,m_clrBack);
  145. }
  146. else
  147. {
  148. rect = CRect(CPoint(0,0),m_sizePic);
  149. pDC->FillSolidRect(rect,m_clrBack);
  150. }
  151. }
  152. pDC->SetBkMode(TRANSPARENT);
  153. // 画网格
  154. if(!CDrawObj::bRuning && !pDC->IsPrinting() && m_bGrid && (!bPrint) )
  155. {
  156. int r = (64+GetRValue(m_clrBack)) % 256;
  157. int g = (64+GetGValue(m_clrBack)) % 256;
  158. int b = (64+GetBValue(m_clrBack)) % 256;
  159. CPen pen(PS_DOT,1,RGB(r,g,b));
  160. CPen* pOldPen = pDC->SelectObject(&pen);
  161. int Width = rect.Width();
  162. int Height = rect.Height();
  163. for(int w=0;w<Width;w+=m_sizeGrid.cx)
  164. {
  165. pDC->MoveTo(rect.left + w-1, rect.top);
  166. pDC->LineTo(rect.left + w-1, rect.bottom);
  167. }
  168. for(int h=0;h<Height;h+=m_sizeGrid.cy)
  169. {
  170. pDC->MoveTo(rect.left , rect.top + h-1);
  171. pDC->LineTo(rect.right, rect.top + h-1);
  172. }
  173. pDC->SelectObject(pOldPen);
  174. DeleteObject( pen );
  175. }
  176. CRect rectClip;
  177. pDC->GetClipBox(&rectClip);
  178. // 画图元
  179. #if GRAPH_DEBUG
  180. if(!m_DrawObjList.IsEmpty())
  181. {
  182. for(POSITION pos = m_DrawObjList.GetHeadPosition();pos!=NULL;)
  183. {
  184. POSITION pos1 = pos ; //要在这里做一个备份
  185. CDrawObj* pObj = m_DrawObjList.GetAt(pos1);
  186. CRect rect = pObj->m_rect;
  187. rect.NormalizeRect();
  188. rect.InflateRect(5,5,5,5);
  189. if(rect.IntersectRect(&rect,&rectClip))
  190. {
  191. if( pObj->m_strCaption=="OCX" )
  192. {
  193. sOCXPath = ((CDrawOCX *)pObj)->m_strOcxPath;
  194. iOcxIndex = ((CDrawOCX *)pObj)->m_nOcxIndex;
  195. rectRet = pObj->m_rect;
  196. }
  197. pObj->Draw(pDC);
  198. }
  199. m_DrawObjList.GetNext(pos);//他会改变 pos的值
  200. }
  201. }
  202. #else
  203. POSITION pos = m_DrawObjList.GetHeadPosition();
  204. while(pos!=NULL)
  205. {
  206. CDrawObj* pObj = m_DrawObjList.GetNext(pos);
  207. CRect rect = pObj->m_rect;
  208. rect.NormalizeRect();
  209. rect.InflateRect(5,5,5,5);
  210. if(rect.IntersectRect(&rect,&rectClip)) pObj->Draw(pDC);
  211. }
  212. #endif
  213. // 画边框
  214. if (!g_bRun && (!bPrint) )
  215. {
  216. CRect rect1;
  217. CBrush brush(RGB(202,202,202));
  218. CBrush *prb;
  219. prb=pDC->SelectObject(&brush);
  220. rect1 = CRect(0,0,m_sizePic.cx,5);
  221. pDC->FillRect(&rect1,&brush);
  222. rect1 = CRect(0,0,5,m_sizePic.cy);
  223. pDC->FillRect(&rect1,&brush);
  224. rect1 = CRect(0,m_sizePic.cy,m_sizePic.cx,m_sizePic.cy-5);
  225. pDC->FillRect(&rect1,&brush);
  226. rect1 = CRect(m_sizePic.cx,0,m_sizePic.cx-5,m_sizePic.cy);
  227. pDC->FillRect(&rect1,&brush);
  228. // 画8根线
  229. CPen pen(PS_SOLID,1,RGB(232,232,232));
  230. CPen* pOldPen = pDC->SelectObject(&pen);
  231. pDC->MoveTo(1, m_sizePic.cy-1);
  232. pDC->LineTo(1, 1);
  233. pDC->LineTo(m_sizePic.cx-1, 1);
  234. CPen pen1(PS_SOLID,1,RGB(64,64,64));
  235. pDC->SelectObject(&pen1);
  236. pDC->MoveTo(1, m_sizePic.cy-1);
  237. pDC->LineTo(m_sizePic.cx, m_sizePic.cy-1);
  238. pDC->LineTo(m_sizePic.cx, 1);
  239. CPen pen2(PS_SOLID,1,RGB(242,242,242));
  240. pDC->SelectObject(&pen2);
  241. pDC->MoveTo(5, m_sizePic.cy-5);
  242. pDC->LineTo(m_sizePic.cx-4, m_sizePic.cy-5);
  243. pDC->LineTo(m_sizePic.cx-4, 5);
  244. CPen pen3(PS_SOLID,1,RGB(96,96,96));
  245. pDC->SelectObject(&pen3);
  246. pDC->MoveTo(4, m_sizePic.cy-5);
  247. pDC->LineTo(4, 4);
  248. pDC->LineTo(m_sizePic.cx-5, 4);
  249. pDC->SelectObject(pOldPen);
  250. pDC->SelectObject(prb);
  251. brush.DeleteObject();
  252. pDC->SetBkMode(OPAQUE);
  253. DeleteObject( pen );
  254. DeleteObject( pen1 );
  255. DeleteObject( pen2 );
  256. DeleteObject( pen3 );
  257. DeleteObject( brush );
  258. }
  259. return rectRet;
  260. }
  261. void CGraph::Print(CDC* pDC)
  262. {
  263. ASSERT(m_pView);
  264. CString sTemp;
  265. int nTemp=0;
  266. CSize size = m_sizePic;
  267. CClientDC dc(m_pView);
  268. CDC dcMem;
  269. dcMem.CreateCompatibleDC(&dc);
  270. CBitmap* pBitmap = new CBitmap;
  271. pBitmap->CreateCompatibleBitmap(&dc,size.cx,size.cy);
  272. CBitmap* pOldBitmap = dcMem.SelectObject(pBitmap);
  273. Draw(&dcMem,sTemp,nTemp,TRUE);
  274. dcMem.InvertRect(CRect(CPoint(0,0),m_sizePic));
  275. for(int y=0;y<size.cy;y++)
  276. {
  277. int nX1 = 0;
  278. COLORREF clrPixel = ::GetPixel(dcMem.m_hDC,0,y);
  279. int nRValue0 = GetRValue(clrPixel);
  280. int nGValue0 = GetGValue(clrPixel);
  281. int nBValue0 = GetBValue(clrPixel);
  282. for(int x=1;x<size.cx;x++)
  283. {
  284. COLORREF clrPixel = ::GetPixel(dcMem.m_hDC,x,y);
  285. int nRValue = GetRValue(clrPixel);
  286. int nGValue = GetGValue(clrPixel);
  287. int nBValue = GetBValue(clrPixel);
  288. int nRDiff = (nRValue<nRValue0)?nRValue0-nRValue:nRValue-nRValue0;
  289. int nGDiff = (nGValue<nGValue0)?nGValue0-nGValue:nGValue-nGValue0;
  290. int nBDiff = (nBValue<nBValue0)?nBValue0-nBValue:nBValue-nBValue0;
  291. if(nRDiff+nGDiff+nBDiff>32)
  292. {
  293. RECT rect;
  294. rect.left = nX1;
  295. rect.top = y;
  296. rect.right = x;
  297. rect.bottom = y+1;
  298. ::SetBkColor(pDC->m_hDC,RGB(nRValue0,nGValue0,nBValue0));
  299. ::ExtTextOut(pDC->m_hDC,0,0,ETO_OPAQUE,&rect,NULL,0,NULL);
  300. nX1 = x;
  301. nRValue0 = nRValue;
  302. nGValue0 = nGValue;
  303. nBValue0 = nBValue;
  304. }
  305. }
  306. RECT rect;
  307. rect.left = nX1;
  308. rect.top = y;
  309. rect.right = x;
  310. rect.bottom = y+1;
  311. ::SetBkColor(pDC->m_hDC,RGB(nRValue0,nGValue0,nBValue0));
  312. ::ExtTextOut(pDC->m_hDC,0,0,ETO_OPAQUE,&rect,NULL,0,NULL);
  313. }
  314. //2011-11-01 add
  315. DeleteObject( pBitmap );
  316. dcMem.SelectObject(pOldBitmap);
  317. dcMem.DeleteDC( );
  318. delete pBitmap;
  319. }
  320. void CGraph::Fresh(void)
  321. {
  322. #if GRAPH_DEBUG
  323. if(!m_DrawObjList.IsEmpty())
  324. {
  325. CRect rect;
  326. for(POSITION pos = m_DrawObjList.GetHeadPosition();pos!=NULL;)
  327. {
  328. POSITION pos1 = pos ; //要在这里做一个备份
  329. if( (int)pos1 == 0xfeeefeee ) break; // 频繁单击画面会出现异常,暂时不知道什么解决
  330. CDrawObj* pObj = m_DrawObjList.GetAt(pos1);
  331. if( (int)pObj == 0xcdcdcdcd || (int)pObj == 0xfeeefeee || (int)pObj == 0x00000001 ) break; // 频繁单击画面会出现异常,暂时不知道什么解决
  332. if( (int)pObj->m_pView == 0xfeeefeee || (int)pObj->m_pView == 0x00000001 ) // 频繁单击画面会出现异常,暂时不知道什么解决
  333. {
  334. TRACE(g_strTRACE0);
  335. break;
  336. }
  337. if( pObj->Fresh(rect) )
  338. InvalidateRect( pObj->m_pView->GetSafeHwnd(),rect,true );
  339. m_DrawObjList.GetNext(pos);//他会改变 pos的值
  340. }
  341. }
  342. #else
  343. POSITION pos = m_DrawObjList.GetHeadPosition();
  344. while(pos!=NULL)
  345. {
  346. CDrawObj* pObj;
  347. pObj = m_DrawObjList.GetNext(pos);
  348. pObj->Fresh();
  349. }
  350. #endif
  351. }