ShapeTree.cpp
上传用户:mosfetic
上传日期:2022-06-16
资源大小:4612k
文件大小:5k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1. // ShapeTree.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DrawSys.h"
  5. #include "ShapeTree.h"
  6. #include "DrawShape.h"
  7. #include "DrawRect.h"
  8. #include "DrawEllipse.h"
  9. #include "DrawCircle.h"
  10. #include "DrawFillRect.h"
  11. #include "DrawFillEllipse.h"
  12. #include "DrawLine.h"
  13. #include "MainFrm.h"
  14. #define WM_CHOOSE (WM_USER+1)
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CShapeTree
  22. CShapeTree::CShapeTree()
  23. {
  24. m_hRectRoot = NULL;
  25. m_hEllipseRoot = NULL;
  26. m_hCircleRoot = NULL;
  27. m_hFillRectRoot = NULL;
  28. m_hFillEllipseRoot = NULL;
  29. m_hLineRoot = NULL;
  30. m_iRectNum = 0;
  31. m_iEllipseNum = 0;
  32. m_iCircleNum = 0;
  33. m_iFillRectNum = 0;
  34. m_iFillEllipseNum = 0;
  35. m_iLineNum = 0;
  36. }
  37. CShapeTree::~CShapeTree()
  38. {
  39. m_lstBitmap.DeleteImageList();
  40. }
  41. BEGIN_MESSAGE_MAP(CShapeTree, CTreeCtrl)
  42. //{{AFX_MSG_MAP(CShapeTree)
  43. ON_WM_CREATE()
  44. ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
  45. ON_WM_RBUTTONDOWN()
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CShapeTree message handlers
  50. void CShapeTree::InsertShape(CDrawShape* pDrawShape, int iIndex)
  51. {
  52. CString strText;
  53. HTREEITEM hItem = NULL;
  54. if(pDrawShape)
  55. {
  56. if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawRect)))
  57. {
  58. if(!m_hRectRoot)
  59. {
  60. m_hRectRoot = InsertItem("矩形", 0, 0);
  61. SetItemData(m_hRectRoot, 0);
  62. }
  63. m_iRectNum++;
  64. MyFormatString(strText, m_iRectNum);
  65. hItem = InsertItem(strText, 0, 0, m_hRectRoot);
  66. SetItemData(hItem, iIndex);
  67. Expand(m_hRectRoot, TVE_EXPAND);
  68. }
  69. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawEllipse)))
  70. {
  71. if(!m_hEllipseRoot)
  72. {
  73. m_hEllipseRoot = InsertItem("椭圆", 1, 1);
  74. SetItemData(m_hEllipseRoot, 0);
  75. }
  76. m_iEllipseNum++;
  77. MyFormatString(strText, m_iEllipseNum);
  78. hItem = InsertItem(strText, 1, 1, m_hEllipseRoot);
  79. SetItemData(hItem, iIndex);
  80. Expand(m_hEllipseRoot, TVE_EXPAND);
  81. }
  82. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawCircle)))
  83. {
  84. if(!m_hCircleRoot)
  85. {
  86. m_hCircleRoot = InsertItem("圆", 2, 2);
  87. SetItemData(m_hCircleRoot, 0);
  88. }
  89. m_iCircleNum++;
  90. MyFormatString(strText, m_iCircleNum);
  91. hItem = InsertItem(strText, 2, 2, m_hCircleRoot);
  92. SetItemData(hItem, iIndex);
  93. Expand(m_hCircleRoot, TVE_EXPAND);
  94. }
  95. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawFillRect)))
  96. {
  97. if(!m_hFillRectRoot)
  98. {
  99. m_hFillRectRoot = InsertItem("填充矩形", 5, 5);
  100. SetItemData(m_hFillRectRoot, 0);
  101. }
  102. m_iFillRectNum++;
  103. MyFormatString(strText, m_iFillRectNum);
  104. hItem = InsertItem(strText, 5, 5, m_hFillRectRoot);
  105. SetItemData(hItem, iIndex);
  106. Expand(m_hFillRectRoot, TVE_EXPAND);
  107. }
  108. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawFillEllipse)))
  109. {
  110. if(!m_hFillEllipseRoot)
  111. {
  112. m_hFillEllipseRoot = InsertItem("填充椭圆", 4, 4);
  113. SetItemData(m_hFillEllipseRoot, 0);
  114. }
  115. m_iFillEllipseNum++;
  116. MyFormatString(strText, m_iFillEllipseNum);
  117. hItem = InsertItem(strText, 4, 4, m_hFillEllipseRoot);
  118. SetItemData(hItem, iIndex);
  119. Expand(m_hFillEllipseRoot, TVE_EXPAND);
  120. }
  121. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawLine)))
  122. {
  123. if(!m_hLineRoot)
  124. {
  125. m_hLineRoot = InsertItem("直线", 3, 3);
  126. SetItemData(m_hLineRoot, 0);
  127. }
  128. m_iLineNum++;
  129. MyFormatString(strText, m_iLineNum);
  130. hItem = InsertItem(strText, 3, 3, m_hLineRoot);
  131. SetItemData(hItem, iIndex);
  132. Expand(m_hLineRoot, TVE_EXPAND);
  133. }
  134. }
  135. }
  136. int CShapeTree::GetID(CDrawShape* pDrawShape)
  137. {
  138. if(pDrawShape)
  139. {
  140. if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawRect)))
  141. {
  142. return m_iRectNum+1;
  143. }
  144. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawEllipse)))
  145. {
  146. return m_iEllipseNum+1;
  147. }
  148. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawCircle)))
  149. {
  150. return m_iCircleNum+1;
  151. }
  152. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawFillRect)))
  153. {
  154. return m_iFillRectNum+1;
  155. }
  156. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawFillEllipse)))
  157. {
  158. return m_iFillEllipseNum+1;
  159. }
  160. else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawLine)))
  161. {
  162. return m_iLineNum+1;
  163. }
  164. }
  165. return 0;
  166. }
  167. void CShapeTree::MyFormatString(CString& strText, int i)
  168. {
  169. strText.Empty();
  170. if(i < 10)
  171. {
  172. strText.Format("00%d#", i);
  173. }
  174. else if(i < 100)
  175. {
  176. strText.Format("0%d#", i);
  177. }
  178. else
  179. {
  180. strText.Format("%d#", i);
  181. }
  182. }
  183. int CShapeTree::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  184. {
  185. if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
  186. return -1;
  187. // TODO: Add your specialized creation code here
  188. m_lstBitmap.Create(IDB_BITMAP_IMAGE, 16, 0, RGB(0, 0, 0));
  189. SetImageList(&m_lstBitmap, TVSIL_NORMAL);
  190. return 0;
  191. }
  192. void CShapeTree::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
  193. {
  194. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  195. // TODO: Add your control notification handler code here
  196. DWORD wNewIndex = 0;
  197. DWORD wOldIndex = 0;
  198. if(pNMTreeView->itemNew.hItem)
  199. {
  200. wNewIndex = GetItemData(pNMTreeView->itemNew.hItem);
  201. }
  202. if(pNMTreeView->itemOld.hItem)
  203. {
  204. wOldIndex = GetItemData(pNMTreeView->itemOld.hItem);
  205. }
  206. CWnd* pWnd = AfxGetMainWnd();
  207. if(wNewIndex > 0)
  208. {
  209. ((CMainFrame*)pWnd)->GetActiveView()->SendMessage(WM_CHOOSE, wNewIndex, wOldIndex);
  210. }
  211. *pResult = 0;
  212. }