PruebaPTreeDlg.cpp
上传用户:wzy3721
上传日期:2021-11-20
资源大小:32k
文件大小:7k
源码类别:

TreeView控件

开发平台:

Visual C++

  1. // PruebaPTreeDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "PruebaPTree.h"
  5. #include "PruebaPTreeDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CPruebaPTreeDlg dialog
  50. CPruebaPTreeDlg::CPruebaPTreeDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CPruebaPTreeDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CPruebaPTreeDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CPruebaPTreeDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CPruebaPTreeDlg)
  63. DDX_Control(pDX, IDC_LB_ITEMS, mc_lbItems);
  64. DDX_Control(pDX, IDC_TC_TREE, mc_tcTree);
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CPruebaPTreeDlg, CDialog)
  68. //{{AFX_MSG_MAP(CPruebaPTreeDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_BN_CLICKED(IDC_BT_COLLAPSE, OnBtCollapse)
  73. ON_BN_CLICKED(IDC_BT_EXPAND, OnBtExpand)
  74. ON_BN_CLICKED(IDC_BT_RELOAD, OnBtReload)
  75. ON_BN_CLICKED(IDC_BT_ITERATE_DEEP, OnBtIterateDeep)
  76. ON_BN_CLICKED(IDC_BT_ITERATE_WIDTH, OnBtIterateWidth)
  77. //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CPruebaPTreeDlg message handlers
  81. BOOL CPruebaPTreeDlg::OnInitDialog()
  82. {
  83. CDialog::OnInitDialog();
  84. // Add "About..." menu item to system menu.
  85. // IDM_ABOUTBOX must be in the system command range.
  86. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  87. ASSERT(IDM_ABOUTBOX < 0xF000);
  88. CMenu* pSysMenu = GetSystemMenu(FALSE);
  89. if (pSysMenu != NULL)
  90. {
  91. CString strAboutMenu;
  92. strAboutMenu.LoadString(IDS_ABOUTBOX);
  93. if (!strAboutMenu.IsEmpty())
  94. {
  95. pSysMenu->AppendMenu(MF_SEPARATOR);
  96. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  97. }
  98. }
  99. // Set the icon for this dialog.  The framework does this automatically
  100. //  when the application's main window is not a dialog
  101. SetIcon(m_hIcon, TRUE); // Set big icon
  102. SetIcon(m_hIcon, FALSE); // Set small icon
  103. // TODO: Add extra initialization here
  104. PopulateTree();
  105. return TRUE;  // return TRUE  unless you set the focus to a control
  106. }
  107. void CPruebaPTreeDlg::OnSysCommand(UINT nID, LPARAM lParam)
  108. {
  109. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  110. {
  111. CAboutDlg dlgAbout;
  112. dlgAbout.DoModal();
  113. }
  114. else
  115. {
  116. CDialog::OnSysCommand(nID, lParam);
  117. }
  118. }
  119. // If you add a minimize button to your dialog, you will need the code below
  120. //  to draw the icon.  For MFC applications using the document/view model,
  121. //  this is automatically done for you by the framework.
  122. void CPruebaPTreeDlg::OnPaint() 
  123. {
  124. if (IsIconic())
  125. {
  126. CPaintDC dc(this); // device context for painting
  127. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  128. // Center icon in client rectangle
  129. int cxIcon = GetSystemMetrics(SM_CXICON);
  130. int cyIcon = GetSystemMetrics(SM_CYICON);
  131. CRect rect;
  132. GetClientRect(&rect);
  133. int x = (rect.Width() - cxIcon + 1) / 2;
  134. int y = (rect.Height() - cyIcon + 1) / 2;
  135. // Draw the icon
  136. dc.DrawIcon(x, y, m_hIcon);
  137. }
  138. else
  139. {
  140. CDialog::OnPaint();
  141. }
  142. }
  143. // The system calls this to obtain the cursor to display while the user drags
  144. //  the minimized window.
  145. HCURSOR CPruebaPTreeDlg::OnQueryDragIcon()
  146. {
  147. return (HCURSOR) m_hIcon;
  148. }
  149. void CPruebaPTreeDlg::PopulateTree()
  150. {
  151. mc_tcTree.DeleteAllItems();
  152. HTREEITEM hti1= NULL, hti2= NULL;
  153. hti1= mc_tcTree.InsertItem("Item 1");
  154. mc_tcTree.InsertItem("Item 11", hti1);
  155. mc_tcTree.InsertItem("Item 12", hti1);
  156. mc_tcTree.InsertItem("Item 13", hti1);
  157. hti1= mc_tcTree.InsertItem("Item 2");
  158. mc_tcTree.InsertItem("Item 21", hti1);
  159. mc_tcTree.InsertItem("Item 22", hti1);
  160. mc_tcTree.InsertItem("Item 23", hti1);
  161. hti1= mc_tcTree.InsertItem("Item 3");
  162. mc_tcTree.InsertItem("Item 31", hti1);
  163. hti2= mc_tcTree.InsertItem("Item 32", hti1);
  164. mc_tcTree.InsertItem("Item 321", hti2);
  165. mc_tcTree.InsertItem("Item 322", hti2);
  166. mc_tcTree.InsertItem("Item 323", hti2);
  167. mc_tcTree.InsertItem("Item 33", hti1);
  168. hti1= mc_tcTree.InsertItem("Item 4");
  169. mc_tcTree.InsertItem("Item 41", hti1);
  170. mc_tcTree.InsertItem("Item 42", hti1);
  171. mc_tcTree.InsertItem("Item 43", hti1);
  172. hti1= mc_tcTree.InsertItem("Item 5");
  173. mc_tcTree.InsertItem("Item 51", hti1);
  174. mc_tcTree.InsertItem("Item 52", hti1);
  175. mc_tcTree.InsertItem("Item 53", hti1);
  176. }
  177. void CPruebaPTreeDlg::OnBtCollapse() 
  178. {
  179. mc_tcTree.SetRedraw(FALSE);
  180. mc_tcTree.ExpandItem(TVI_ROOT, TVE_COLLAPSE);
  181. mc_tcTree.SetRedraw(TRUE);
  182. }
  183. void CPruebaPTreeDlg::OnBtExpand() 
  184. {
  185. mc_tcTree.SetRedraw(FALSE);
  186. mc_tcTree.ExpandItem();
  187. mc_tcTree.SetRedraw(TRUE);
  188. }
  189. void CPruebaPTreeDlg::OnBtReload() 
  190. {
  191. mc_tcTree.SaveStatus();
  192. PopulateTree();
  193. mc_tcTree.RestoreStatus();
  194. }
  195. void CPruebaPTreeDlg::OnBtIterateDeep() 
  196. {
  197. mc_lbItems.ResetContent();
  198. mc_tcTree.Initialize(true);
  199. HTREEITEM hItem= mc_tcTree.GetNext();
  200. while (hItem != NULL)
  201. {
  202. mc_lbItems.AddString(mc_tcTree.GetItemText(hItem));
  203. hItem= mc_tcTree.GetNext();
  204. }
  205. }
  206. void CPruebaPTreeDlg::OnBtIterateWidth() 
  207. {
  208. mc_lbItems.ResetContent();
  209. mc_tcTree.Initialize(false);
  210. HTREEITEM hItem= mc_tcTree.GetNext();
  211. while (hItem != NULL)
  212. {
  213. mc_lbItems.AddString(mc_tcTree.GetItemText(hItem));
  214. hItem= mc_tcTree.GetNext();
  215. }
  216. }
  217. // Force compilation to detect errors
  218. template class TTCExpand<CTreeCtrl>;
  219. template class TTCPersist<CTreeCtrl>;
  220. template class TTCIterator<CTreeCtrl>;