WZDTVIEW.CPP
上传用户:leishk
上传日期:2022-03-16
资源大小:3649k
文件大小:5k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. // WzdTView.cpp : implementation of the CWzdTreeView class
  2. //
  3. #include "stdafx.h"
  4. #include "Wzd.h"
  5. #include "WzdDoc.h"
  6. #include "WzdTView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CWzdTreeView
  14. IMPLEMENT_DYNCREATE(CWzdTreeView, CTreeView)
  15. BEGIN_MESSAGE_MAP(CWzdTreeView, CTreeView)
  16. //{{AFX_MSG_MAP(CWzdTreeView)
  17. ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
  18. //}}AFX_MSG_MAP
  19. // Standard printing commands
  20. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  21. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  22. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CWzdTreeView construction/destruction
  26. CWzdTreeView::CWzdTreeView()
  27. {
  28. // set the CTreeCtrl attributes
  29. m_dwDefaultStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_HASLINES 
  30. | TVS_SHOWSELALWAYS | TVS_LINESATROOT | TVS_HASBUTTONS;
  31. }
  32. CWzdTreeView::~CWzdTreeView()
  33. {
  34. }
  35. BOOL CWzdTreeView::PreCreateWindow(CREATESTRUCT& cs)
  36. {
  37. // TODO: Modify the Window class or styles here by modifying
  38. //  the CREATESTRUCT cs
  39. return CTreeView::PreCreateWindow(cs);
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CWzdTreeView drawing
  43. void CWzdTreeView::OnDraw(CDC* pDC)
  44. {
  45. CWzdDoc* pDoc = GetDocument();
  46. ASSERT_VALID(pDoc);
  47. // TODO: add draw code for native data here
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CWzdTreeView printing
  51. BOOL CWzdTreeView::OnPreparePrinting(CPrintInfo* pInfo)
  52. {
  53. // default preparation
  54. return DoPreparePrinting(pInfo);
  55. }
  56. void CWzdTreeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  57. {
  58. // TODO: add extra initialization before printing
  59. }
  60. void CWzdTreeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  61. {
  62. // TODO: add cleanup after printing
  63. }
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CWzdTreeView diagnostics
  66. #ifdef _DEBUG
  67. void CWzdTreeView::AssertValid() const
  68. {
  69. CView::AssertValid();
  70. }
  71. void CWzdTreeView::Dump(CDumpContext& dc) const
  72. {
  73. CView::Dump(dc);
  74. }
  75. CWzdDoc* CWzdTreeView::GetDocument() // non-debug version is inline
  76. {
  77. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWzdDoc)));
  78. return (CWzdDoc*)m_pDocument;
  79. }
  80. #endif //_DEBUG
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CWzdTreeView message handlers
  83. void CWzdTreeView::OnInitialUpdate() 
  84. {
  85. CTreeView::OnInitialUpdate();
  86. // set icons
  87. m_ImageList.Create(IDB_SMALL_BITMAP, 16, 1, RGB(0,0,0));
  88. GetTreeCtrl().SetImageList(&m_ImageList, TVSIL_NORMAL);
  89. }
  90. void CWzdTreeView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  91. {
  92. if (lHint&LIST_VIEW_ONLY)
  93. {
  94. return;
  95. }
  96. HTREEITEM hitem;
  97. CWzdInfo *pInfo;
  98. switch (lHint)
  99. {
  100. case TREE_VIEW_SELECT:
  101. pInfo=GetDocument()->GetSelection();
  102. if ((hitem = FindTreeItem(GetTreeCtrl().GetChildItem(GetTreeCtrl().GetRootItem()), pInfo->m_nObjectID))!=NULL)
  103. {
  104. GetTreeCtrl().SelectItem(hitem);
  105. }
  106. break;
  107. case TREE_VIEW_UP_LEVEL:
  108. hitem=GetTreeCtrl().GetSelectedItem();
  109. hitem=GetTreeCtrl().GetParentItem(hitem);
  110. GetTreeCtrl().SelectItem(hitem);
  111. break;
  112. default:
  113. GetTreeCtrl().DeleteAllItems();
  114. AddBranch(TVI_ROOT, GetDocument()->GetWzdList());
  115. break;
  116. }
  117. }
  118. // recurse until all CWzdInfo items are in a leaf
  119. void CWzdTreeView::AddBranch(HTREEITEM hTreeItem, CList<CWzdInfo*,CWzdInfo*> *pList)
  120. {
  121. for (POSITION pos = pList->GetHeadPosition(); pos;)
  122. {
  123. CWzdInfo *pInfo = pList->GetNext(pos);
  124. if (pInfo->m_nCategory!=CWzdInfo::FILE)
  125. {
  126. HTREEITEM hTreeItemx=AddLeaf(hTreeItem, pInfo);
  127. if (pList->GetCount())
  128. {
  129. AddBranch(hTreeItemx, &pInfo->m_list);
  130. }
  131. }
  132. }
  133. }
  134. // add leaf to tree
  135. HTREEITEM CWzdTreeView::AddLeaf(HTREEITEM hTreeItem, CWzdInfo *pInfo)
  136. {
  137. TV_INSERTSTRUCT tvstruct;
  138. tvstruct.hParent = hTreeItem;
  139. tvstruct.hInsertAfter = TVI_LAST;
  140. tvstruct.item.iImage = tvstruct.item.iSelectedImage = pInfo->m_nObjectID;
  141. tvstruct.item.pszText = (char *)LPCTSTR(pInfo->m_sName);
  142. tvstruct.item.mask = TVIF_IMAGE |TVIF_SELECTEDIMAGE | TVIF_PARAM  | TVIF_TEXT;
  143. tvstruct.item.lParam=(DWORD)pInfo;
  144. return GetTreeCtrl().InsertItem(&tvstruct);
  145. }
  146. // recurse until item found or items expended
  147. HTREEITEM CWzdTreeView::FindTreeItem(HTREEITEM hTreeItem, long nObjectID)
  148. {
  149. while (hTreeItem != NULL)
  150. {
  151. CWzdInfo *pInfo=(CWzdInfo *)GetTreeCtrl().GetItemData(hTreeItem);
  152. if (pInfo->m_nObjectID==nObjectID)
  153. {
  154. return hTreeItem;
  155. }
  156. else if (GetTreeCtrl().ItemHasChildren(hTreeItem))
  157. {
  158. HTREEITEM hTreeItemx = FindTreeItem(GetTreeCtrl().GetChildItem(hTreeItem), nObjectID);
  159. if (hTreeItemx)
  160. {
  161. return hTreeItemx;
  162. }
  163. }
  164. else
  165. {
  166. hTreeItem = GetTreeCtrl().GetNextItem(hTreeItem, TVGN_NEXT);
  167. }
  168. return NULL;
  169. }
  170. // new selection, change listview!
  171. void CWzdTreeView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
  172. {
  173. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  174. GetDocument()->SaveSelection((CWzdInfo *)pNMTreeView->itemNew.lParam,LIST_VIEW_ONLY);
  175. *pResult = 0;
  176. }