VirtualListView.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:7k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // VirtualListView.cpp : implementation of the CVirtualListView class
  2. //
  3. // This file is a part of the XTREME TOOLKIT PRO MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "VirtualList.h"
  22. #include "VirtualListDoc.h"
  23. #include "VirtualListView.h"
  24. #include "DialogRowsCount.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. class CVirtualRecord : public CXTPReportRecord
  31. {
  32. public:
  33. CVirtualRecord()
  34. {
  35. AddItem(new CXTPReportRecordItem());
  36. AddItem(new CXTPReportRecordItem());
  37. AddItem(new CXTPReportRecordItem());
  38. }
  39. void GetItemMetrics (XTP_REPORTRECORDITEM_DRAWARGS* pDrawArgs, XTP_REPORTRECORDITEM_METRICS* pItemMetrics)
  40. {
  41. CXTPReportColumnOrder* pSortOrder = pDrawArgs->pControl->GetColumns()->GetSortOrder();
  42. BOOL bDecreasing = pSortOrder->GetCount() > 0 && pSortOrder->GetAt(0)->IsSortedDecreasing();
  43. CString strColumn = pDrawArgs->pColumn->GetCaption();
  44. int nIndex = pDrawArgs->pRow->GetIndex();
  45. int nCount = pDrawArgs->pControl->GetRows()->GetCount();
  46. pItemMetrics->strText.Format(_T("%s - Row %i"), strColumn, bDecreasing? nCount - nIndex: nIndex + 1);
  47. CVirtualListView* pView = DYNAMIC_DOWNCAST(CVirtualListView, pDrawArgs->pControl->GetParent());
  48. if (pView && pView->m_bColorize)
  49. {
  50. // Execute this code to apply predefined formatting.  
  51. // To turn on automatic formatting, select it from the menu
  52. if (nIndex % 2)
  53. {
  54. pItemMetrics->clrBackground = RGB(245, 245, 245);
  55. }
  56. }
  57. if (pView && pView->m_bSimpleTree)
  58. {
  59. // Demonstrate possibility to organize one-level tree
  60. if (nIndex % 10 == 0)
  61. {
  62. POSITION pos = pView->m_lstCollapsed.Find(nIndex);
  63. pDrawArgs->pRow->SetExpanded(pos == NULL);
  64. if (pDrawArgs->pColumn->GetIndex() == 0)
  65. {
  66. pItemMetrics->nVirtRowLevel = 0;
  67. pItemMetrics->nVirtRowFlags = xtpVirtRowHasChildren; // row has children
  68. }
  69. pItemMetrics->nVirtRowFlags |= xtpVirtRowLastChild; // row is the last child
  70. }
  71. else
  72. {
  73. pDrawArgs->pRow->SetExpanded(FALSE);
  74. if (pDrawArgs->pColumn->GetIndex() == 0)
  75. {
  76. pItemMetrics->nVirtRowLevel = 1;
  77. pItemMetrics->nVirtRowFlags = 0; 
  78. }
  79. if (nIndex % 10 == 9)
  80. {
  81. pItemMetrics->nVirtRowFlags |= xtpVirtRowLastChild;
  82. }
  83. }
  84. }
  85. }
  86. };
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CVirtualListView
  89. IMPLEMENT_DYNCREATE(CVirtualListView, CXTPReportView)
  90. BEGIN_MESSAGE_MAP(CVirtualListView, CXTPReportView)
  91. //{{AFX_MSG_MAP(CVirtualListView)
  92. ON_WM_CREATE()
  93. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  94. ON_COMMAND(ID_REPORT_SETROWSCOUNT, OnReportSetrowscount)
  95. ON_COMMAND(ID_REPORT_COLORIZE, OnReportColorize)
  96. ON_UPDATE_COMMAND_UI(ID_REPORT_COLORIZE, OnUpdateReportColorize)
  97. ON_COMMAND(ID_REPORT_TREE, OnReportSimpleTree)
  98. ON_UPDATE_COMMAND_UI(ID_REPORT_TREE, OnUpdateReportSimpleTree)
  99. //}}AFX_MSG_MAP
  100. // Standard printing commands
  101. ON_COMMAND(ID_FILE_PRINT, CXTPReportView::OnFilePrint)
  102. ON_COMMAND(ID_FILE_PRINT_DIRECT, CXTPReportView::OnFilePrint)
  103. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CXTPReportView::OnFilePrintPreview)
  104. ON_NOTIFY(NM_CLICK, XTP_ID_REPORT_CONTROL, OnReportItemClick)
  105. END_MESSAGE_MAP()
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CVirtualListView construction/destruction
  108. CVirtualListView::CVirtualListView()
  109. {
  110. m_bColorize = FALSE;
  111. m_bSimpleTree = FALSE;
  112. }
  113. CVirtualListView::~CVirtualListView()
  114. {
  115. }
  116. BOOL CVirtualListView::PreCreateWindow(CREATESTRUCT& cs)
  117. {
  118. // TODO: Modify the Window class or styles here by modifying
  119. //  the CREATESTRUCT cs
  120. return CXTPReportView::PreCreateWindow(cs);
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CVirtualListView diagnostics
  124. #ifdef _DEBUG
  125. void CVirtualListView::AssertValid() const
  126. {
  127. CView::AssertValid();
  128. }
  129. void CVirtualListView::Dump(CDumpContext& dc) const
  130. {
  131. CView::Dump(dc);
  132. }
  133. CVirtualListDoc* CVirtualListView::GetDocument() // non-debug version is inline
  134. {
  135. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVirtualListDoc)));
  136. return (CVirtualListDoc*)m_pDocument;
  137. }
  138. #endif //_DEBUG
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CVirtualListView message handlers
  141. int CVirtualListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  142. {
  143. if (CXTPReportView::OnCreate(lpCreateStruct) == -1)
  144. return -1;
  145. CXTPReportControl& wndReport = GetReportCtrl();
  146. wndReport.GetReportHeader()->AllowColumnRemove(FALSE);
  147. wndReport.AddColumn(new CXTPReportColumn(0, _T("Column 1"), 50));
  148. wndReport.AddColumn(new CXTPReportColumn(1, _T("Column 2"), 50));
  149. wndReport.AddColumn(new CXTPReportColumn(2, _T("Column 3"), 50));
  150. wndReport.SetVirtualMode(new CVirtualRecord(), 1000000);
  151. wndReport.Populate();
  152. return 0;
  153. }
  154. void CVirtualListView::OnReportItemClick(NMHDR * pNotifyStruct, LRESULT * /*result*/)
  155. {
  156. XTP_NM_REPORTRECORDITEM* pItemNotify = (XTP_NM_REPORTRECORDITEM*) pNotifyStruct;
  157. if (!pItemNotify->pRow || !pItemNotify->pColumn)
  158. return;
  159. TRACE(_T("Click on row %d, col %dn"),
  160. pItemNotify->pRow->GetIndex(), pItemNotify->pColumn->GetItemIndex());
  161. }
  162. void CVirtualListView::OnEditCopy()
  163. {
  164. CXTPReportSelectedRows* pSelectedRows = GetReportCtrl().GetSelectedRows();
  165. POSITION pos = pSelectedRows->GetFirstSelectedRowPosition();
  166. while (pos)
  167. {
  168. int nIndex = pSelectedRows->GetNextSelectedRow(pos)->GetIndex();
  169. TRACE(_T("%i, "), nIndex + 1);
  170. }
  171. }
  172. void CVirtualListView::OnReportSetrowscount()
  173. {
  174. CDialogRowsCount dr;
  175. if (dr.DoModal() == IDOK)
  176. {
  177. GetReportCtrl().SetVirtualMode(new CVirtualRecord(), dr.m_nRowsCount);
  178. GetReportCtrl().Populate();
  179. }
  180. }
  181. void CVirtualListView::OnReportColorize()
  182. {
  183. m_bColorize = !m_bColorize;
  184. GetReportCtrl().RedrawControl();
  185. }
  186. void CVirtualListView::OnUpdateReportColorize(CCmdUI* pCmdUI)
  187. {
  188. pCmdUI->SetCheck(m_bColorize);
  189. }
  190. void CVirtualListView::OnReportSimpleTree()
  191. {
  192. m_bSimpleTree = !m_bSimpleTree;
  193. GetReportCtrl().GetColumns()->GetAt(0)->SetTreeColumn(m_bSimpleTree);
  194. if (!m_bSimpleTree)
  195. {
  196. m_lstCollapsed.RemoveAll();
  197. }
  198. GetReportCtrl().RedrawControl();
  199. }
  200. void CVirtualListView::OnUpdateReportSimpleTree(CCmdUI* pCmdUI)
  201. {
  202. pCmdUI->SetCheck(m_bSimpleTree);
  203. }