HStroke.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // HStroke.cpp: implementation of the HStroke class.
  2. // Download by http://www.codefans.net
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "HDraw.h"
  6. #include "HStroke.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. IMPLEMENT_SERIAL(HStroke, CObject, 1 )
  16. HStroke::HStroke()
  17. {
  18. m_penColor = RGB(255, 0, 0);
  19. m_penWidth = 2;
  20. m_penStyle = PS_SOLID;
  21. m_bHighLight = FALSE;
  22. m_bSelected = FALSE;
  23. }
  24. HStroke::~HStroke()
  25. {
  26. }
  27. void HStroke::DrawStroke(CDC *pDC)
  28. {
  29. CPen *pOld, pNew;
  30. pNew.CreatePen(m_penStyle, m_penWidth, m_penColor);
  31. pOld = pDC->SelectObject(&pNew);
  32. _Draw(pDC);
  33. if((m_bSelected||m_bHighLight) && m_points.GetSize() == 2){
  34. m_tracker.m_nStyle = CRectTracker::resizeInside;
  35. m_tracker.m_rect.SetRect(m_points.GetAt(0), m_points.GetAt(1));
  36. m_tracker.m_rect.NormalizeRect();
  37. m_tracker.Draw(pDC);
  38. }
  39. pDC->SelectObject(pOld);
  40. }
  41. void HStroke::ReDrawStroke(CDC *pDC, CPoint point)
  42. {
  43. }
  44. void HStroke::_Draw(CDC *pDC)
  45. {
  46. }
  47. void HStroke::SetCurrentPoint(CPoint point)
  48. {
  49. m_points.Add(point);
  50. }
  51. void HStroke::Serialize(CArchive& ar)
  52. {
  53. if(ar.IsStoring()){
  54. int enumIndex = m_picType;
  55. ar<<enumIndex<<m_penWidth<<m_penColor;
  56. m_points.Serialize(ar);
  57. }
  58. else{
  59. int enumIndex;
  60. ar>>enumIndex>>m_penWidth>>m_penColor;
  61. m_picType = (enum HPicType)enumIndex;
  62. m_points.Serialize(ar);
  63. }
  64. }
  65. //////////////////////////////////////////////////////////////////////
  66. // HStrokeLine Class
  67. //////////////////////////////////////////////////////////////////////
  68. //////////////////////////////////////////////////////////////////////
  69. // Construction/Destruction
  70. //////////////////////////////////////////////////////////////////////
  71. IMPLEMENT_SERIAL(HStrokeLine, CObject, 1)
  72. HStrokeLine::HStrokeLine()
  73. {
  74. m_picType = PIC_line;
  75. m_tracker.m_picType = m_picType;
  76. }
  77. HStrokeLine::~HStrokeLine()
  78. {
  79. }
  80. void HStrokeLine::SetCurrentPoint(CPoint point){
  81. //空
  82. if(m_points.GetSize() < 2){
  83. m_points.Add(point);
  84. }
  85. else{
  86. m_points.ElementAt(1) = point;
  87. }
  88. }
  89. void HStrokeLine::DrawStroke(CDC *pDC){
  90. //左上->右下(0) or 左下-右上(1)
  91. // (x1-x2)*(y1-y2) > 0
  92. m_tracker.m_picExtra = (m_points.GetAt(0).x - m_points.GetAt(1).x) * (m_points.GetAt(0).y - m_points.GetAt(1).y) < 0;
  93. HStroke::DrawStroke(pDC);
  94. }
  95. void HStrokeLine::_Draw(CDC *pDC){
  96. pDC->MoveTo(m_points.GetAt(0));
  97. pDC->LineTo(m_points.GetAt(1));
  98. }
  99. void HStrokeLine::ReDrawStroke(CDC *pDC, CPoint point){
  100. if(m_points.GetSize() < 2){
  101. if(m_points.GetSize() == 1){
  102. m_points.Add(point);
  103. DrawStroke(pDC);
  104. }
  105. return ;
  106. }
  107. pDC->SetROP2(R2_NOTXORPEN);
  108. DrawStroke(pDC);
  109. SetCurrentPoint(point);
  110. DrawStroke(pDC);
  111. }
  112. BOOL HStrokeLine::IsPointIn(const CPoint &point){
  113. int x1 = point.x - m_points.GetAt(0).x;
  114. int x2 = point.x - m_points.GetAt(1).x;
  115. int y1 = point.y - m_points.GetAt(0).y;
  116. int y2 = point.y - m_points.GetAt(1).y;
  117. //y1/x1=y2/x2 => x1*y2=x2*y1
  118. int measure = x1*y2 - x2*y1;
  119. //tolerable distance
  120. int rule = abs(m_points.GetAt(1).x - m_points.GetAt(0).x)
  121. +abs(m_points.GetAt(0).y - m_points.GetAt(1).y);
  122. rule *= m_penWidth;
  123. if(measure < rule && measure > -rule){
  124. //between the two points
  125. if(x1 * x2 < 0)
  126. return TRUE;
  127. else
  128. return FALSE;
  129. }
  130. else
  131. return FALSE;
  132. }
  133. //////////////////////////////////////////////////////////////////////
  134. // HStrokePoly Class
  135. //////////////////////////////////////////////////////////////////////
  136. //////////////////////////////////////////////////////////////////////
  137. // Construction/Destruction
  138. //////////////////////////////////////////////////////////////////////
  139. IMPLEMENT_SERIAL(HStrokePoly, HStroke, 1)
  140. HStrokePoly::HStrokePoly()
  141. {
  142. m_picType = PIC_poly;
  143. }
  144. HStrokePoly::~HStrokePoly()
  145. {
  146. }
  147. void HStrokePoly::SetCurrentPoint(CPoint point){
  148. m_points.Add(point);
  149. }
  150. void HStrokePoly::_Draw(CDC *pDC){
  151. if(m_points.GetSize() < 2)
  152. return ;
  153. pDC->MoveTo(m_points.GetAt(0));
  154. for(int i = 1; i < m_points.GetSize(); i ++)
  155. pDC->LineTo(m_points.GetAt(i));
  156. }
  157. void HStrokePoly::ReDrawStroke(CDC *pDC, CPoint point){
  158. if(m_points.GetSize() < 2)
  159. SetCurrentPoint(point);
  160. DrawStroke(pDC);
  161. SetCurrentPoint(point);
  162. }
  163. BOOL HStrokePoly::IsPointIn(const CPoint &point){
  164. for(int i = 0; i < m_points.GetSize(); i ++){
  165. if(m_points.GetAt(i) == point)
  166. return TRUE;
  167. }
  168. return FALSE;
  169. }
  170. //////////////////////////////////////////////////////////////////////
  171. // HStrokeRect Class
  172. //////////////////////////////////////////////////////////////////////
  173. //////////////////////////////////////////////////////////////////////
  174. // Construction/Destruction
  175. //////////////////////////////////////////////////////////////////////
  176. IMPLEMENT_SERIAL(HStrokeRect, HStrokeLine, 1)
  177. HStrokeRect::HStrokeRect()
  178. {
  179. m_picType = PIC_rect;
  180. m_tracker.m_picType = m_picType;
  181. }
  182. HStrokeRect::~HStrokeRect()
  183. {
  184. }
  185. void HStrokeRect::_Draw(CDC *pDC){
  186. if(m_points.GetSize() < 2)
  187. return ;
  188. pDC->SelectStockObject(NULL_BRUSH);
  189. pDC->Rectangle(m_points.GetAt(0).x,
  190. m_points.GetAt(0).y,
  191. m_points.GetAt(1).x,
  192. m_points.GetAt(1).y);
  193. }
  194. void HStrokeRect::ReDrawStroke(CDC *pDC, CPoint point){
  195. if(m_points.GetSize() < 2){
  196. if(m_points.GetSize() == 1){
  197. SetCurrentPoint(point);
  198. DrawStroke(pDC);
  199. }
  200. return ;
  201. }
  202. pDC->SetROP2(R2_NOTXORPEN);
  203. DrawStroke(pDC);
  204. SetCurrentPoint(point);
  205. DrawStroke(pDC);
  206. }
  207. BOOL HStrokeRect::IsPointIn(const CPoint &point){
  208. CRect rect(m_points.GetAt(0), m_points.GetAt(1));
  209. CRect rect2(rect.left + 5, rect.top+5, rect.right-5, rect.bottom-5);
  210. if(rect.PtInRect(point) && !rect2.PtInRect(point))
  211. return TRUE;
  212. else
  213. return FALSE;
  214. }
  215. //////////////////////////////////////////////////////////////////////
  216. // HStrokeEllipse Class
  217. //////////////////////////////////////////////////////////////////////
  218. //////////////////////////////////////////////////////////////////////
  219. // Construction/Destruction
  220. //////////////////////////////////////////////////////////////////////
  221. IMPLEMENT_SERIAL(HStrokeEllipse, CObject, 1)
  222. HStrokeEllipse::HStrokeEllipse()
  223. {
  224. m_picType = PIC_ellipse;
  225. m_tracker.m_picType = m_picType;
  226. }
  227. HStrokeEllipse::~HStrokeEllipse()
  228. {
  229. }
  230. void HStrokeEllipse::_Draw(CDC *pDC){
  231. if(m_points.GetSize() < 2)
  232. return ;
  233. pDC->SelectStockObject(NULL_BRUSH);
  234. pDC->Ellipse(m_points.GetAt(0).x,
  235. m_points.GetAt(0).y,
  236. m_points.GetAt(1).x,
  237. m_points.GetAt(1).y);
  238. }
  239. void HStrokeEllipse::ReDrawStroke(CDC *pDC, CPoint point){
  240. if(m_points.GetSize() < 2){
  241. if(m_points.GetSize() == 1){
  242. SetCurrentPoint(point);
  243. DrawStroke(pDC);
  244. }
  245. return ;
  246. }
  247. pDC->SetROP2(R2_NOTXORPEN);
  248. DrawStroke(pDC);
  249. SetCurrentPoint(point);
  250. DrawStroke(pDC);
  251. }
  252. BOOL HStrokeEllipse::IsPointIn(const CPoint &point){
  253. //calculate a,b,c of Ellipse
  254. int _2a = abs(m_points.GetAt(0).x - m_points.GetAt(1).x);
  255. int _2b = abs(m_points.GetAt(0).y - m_points.GetAt(1).y);
  256. double c = sqrt((double)abs(_2a*_2a - _2b*_2b))/2;
  257. //calculate two points
  258. double x1,y1,x2,y2;
  259. if(_2a > _2b){
  260. x1 = (double)(m_points.GetAt(0).x + m_points.GetAt(1).x)/2 - c;
  261. x2 = x1 + 2*c;
  262. y1 = y2 = (m_points.GetAt(0).y + m_points.GetAt(1).y)/2;
  263. }
  264. else{
  265. _2a = _2b;
  266. x1 = x2 = (m_points.GetAt(0).x + m_points.GetAt(1).x)/2;
  267. y1 = (m_points.GetAt(0).y + m_points.GetAt(1).y)/2 - c;
  268. y2 = y1 + 2*c;
  269. }
  270. //distance(point - p1) + distance(point - p2) = 2*a;
  271. double measure = sqrt((x1 - point.x)*(x1-point.x) + (y1 - point.y)*(y1-point.y) )
  272. + sqrt( (point.x - x2)*(point.x - x2) + (point.y - y2)*(point.y - y2))
  273. - _2a;
  274. double rule = 4*m_penWidth;
  275. if(measure < rule && measure > -rule)
  276. return TRUE;
  277. else
  278. return FALSE;
  279. }
  280. //////////////////////////////////////////////////////////////////////
  281. // HStrokeText Class
  282. //////////////////////////////////////////////////////////////////////
  283. //////////////////////////////////////////////////////////////////////
  284. // Construction/Destruction
  285. //////////////////////////////////////////////////////////////////////
  286. IMPLEMENT_SERIAL(HStrokeText, HStrokeLine, 1)
  287. HStrokeText::HStrokeText()
  288. {
  289. m_picType = PIC_text;
  290. }
  291. HStrokeText::HStrokeText(CString info)
  292. {
  293. m_text = info;
  294. m_picType = PIC_text;
  295. }
  296. HStrokeText::~HStrokeText()
  297. {
  298. }
  299. void HStrokeText::DrawStroke(CDC *pDC){
  300. if(m_points.GetSize() < 2)
  301. return ;
  302. CRect rect(m_points.GetAt(0).x,
  303. m_points.GetAt(0).y,
  304. m_points.GetAt(1).x,
  305. m_points.GetAt(1).y);
  306. pDC->SetBkMode(TRANSPARENT);
  307. pDC->SetTextColor(m_penColor);
  308. CFont textFont, *pOldFont;
  309. textFont.CreatePointFont(m_penWidth*100, "Times New Roman");
  310. pOldFont = pDC->SelectObject(&textFont);
  311. pDC->DrawText(m_text, rect, DT_LEFT);
  312. pDC->SelectObject(pOldFont);
  313. }
  314. void HStrokeText::ReDrawStroke(CDC *pDC, CPoint point){
  315. if(m_points.GetSize() < 2){
  316. if(m_points.GetSize() == 1){
  317. SetCurrentPoint(point);
  318. DrawStroke(pDC);
  319. }
  320. return ;
  321. }
  322. pDC->SetROP2(R2_XORPEN);
  323. DrawStroke(pDC);
  324. SetCurrentPoint(point);
  325. DrawStroke(pDC);
  326. }
  327. void HStrokeText::Serialize(CArchive& ar)
  328. {
  329. HStroke::Serialize(ar);
  330. if(ar.IsStoring())
  331. ar<<m_text;
  332. else
  333. ar>>m_text;
  334. }
  335. BOOL HStroke::IsPointIn(const CPoint &point)
  336. {
  337. return false;
  338. }
  339. void HStroke::SetHighLight(BOOL bHL)
  340. {
  341. m_bHighLight = bHL;
  342. }
  343. BOOL HStroke::IsHightLight()
  344. {
  345. return m_bHighLight;
  346. }
  347. void HStroke::Move(int x, int y)
  348. {
  349. for(int i = 0; i < m_points.GetSize(); i ++){
  350. m_points.ElementAt(i).x += x;
  351. m_points.ElementAt(i).y += y;
  352. }
  353. }
  354. void HStroke::ReSize(CRect newPos)
  355. {
  356. ASSERT(m_points.GetSize() >= 2);
  357. //左下到右上
  358. if((m_points[0].x - m_points[1].x) * (m_points[0].y - m_points[1].y) < 0)
  359. {
  360. m_points.ElementAt(0) = CPoint(newPos.left, newPos.bottom);
  361. m_points.ElementAt(1) = CPoint(newPos.right, newPos.top);
  362. }
  363. else
  364. {
  365. m_points.ElementAt(0) = CPoint(newPos.left, newPos.top);
  366. m_points.ElementAt(1) = CPoint(newPos.right, newPos.bottom);
  367. }
  368. }
  369. HStrokeSelect::HStrokeSelect()
  370. {
  371. m_picType = PIC_select;
  372. }
  373. void HStrokeSelect::DrawStroke(CDC *pDC)
  374. {
  375. m_penColor = RGB(255,0,0);
  376. m_penWidth = 1;
  377. m_penStyle = PS_DASH;
  378. HStrokeRect::DrawStroke(pDC);
  379. }
  380. HStrokeTracker::HStrokeTracker():CRectTracker(){
  381. m_picType = PIC_rect;
  382. }
  383. void HStrokeTracker::Draw(CDC* pDC) const
  384. {
  385. CRect rect;
  386. CRectTracker::Draw(pDC);
  387. //直线
  388. if((m_picType == PIC_line) && ((m_nStyle&(resizeInside|resizeOutside))!=0))
  389. {
  390. UINT mask = GetHandleMask();
  391. for (int i = 0; i < 8; ++i)
  392. {
  393. if (mask & (1<<i))
  394. {
  395. int p1, p2;
  396. //左上+右下
  397. if(m_picExtra == 0)
  398. {
  399. p1 = 1, p2 = 4;
  400. }
  401. //左下+右上
  402. else
  403. {
  404. p1 = 2, p2 = 8;
  405. }
  406. if( ((1<<i) == p1) || ((1<<i) == p2))
  407. {
  408. GetHandleRect((TrackerHit)i, &rect);
  409. pDC->FillSolidRect(rect, RGB(0, 0, 0));
  410. }
  411. else
  412. {
  413. GetHandleRect((TrackerHit)i, &rect);
  414. pDC->FillSolidRect(rect, RGB(255, 255, 255));
  415. }
  416. }
  417. }
  418. }
  419. }