TreeMenuDemo.cpp
上传用户:rxhxxy
上传日期:2007-01-02
资源大小:72k
文件大小:8k
源码类别:

TreeView控件

开发平台:

Visual C++

  1. // TreeMenuDemo.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "TreeMenuDemo.h"
  5. #include "MainFrm.h"
  6. #include "ChildFrm.h"
  7. #include "TreeMenuDemoDoc.h"
  8. #include "TreeMenuDemoView.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CTreeMenuDemoApp
  16. BEGIN_MESSAGE_MAP(CTreeMenuDemoApp, CWinApp)
  17. //{{AFX_MSG_MAP(CTreeMenuDemoApp)
  18. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  19. // NOTE - the ClassWizard will add and remove mapping macros here.
  20. //    DO NOT EDIT what you see in these blocks of generated code!
  21. //}}AFX_MSG_MAP
  22. // Standard file based document commands
  23. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  24. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CTreeMenuDemoApp construction
  28. CTreeMenuDemoApp::CTreeMenuDemoApp()
  29. {
  30. // TODO: add construction code here,
  31. // Place all significant initialization in InitInstance
  32. }
  33. /////////////////////////////////////////////////////////////////////////////
  34. // The one and only CTreeMenuDemoApp object
  35. CTreeMenuDemoApp theApp;
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CTreeMenuDemoApp initialization
  38. BOOL CTreeMenuDemoApp::InitInstance()
  39. {
  40. // Standard initialization
  41. // If you are not using these features and wish to reduce the size
  42. //  of your final executable, you should remove from the following
  43. //  the specific initialization routines you do not need.
  44. #ifdef _AFXDLL
  45. Enable3dControls(); // Call this when using MFC in a shared DLL
  46. #else
  47. Enable3dControlsStatic(); // Call this when linking to MFC statically
  48. #endif
  49. // Change the registry key under which our settings are stored.
  50. // You should modify this string to be something appropriate
  51. // such as the name of your company or organization.
  52. SetRegistryKey(_T("Local AppWizard-Generated Applications"));
  53. LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  54. // Register the application's document templates.  Document templates
  55. //  serve as the connection between documents, frame windows and views.
  56. CMultiDocTemplate* pDocTemplate;
  57. pDocTemplate = new CMultiDocTemplate(
  58. IDR_TREEMETYPE,
  59. RUNTIME_CLASS(CTreeMenuDemoDoc),
  60. RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  61. RUNTIME_CLASS(CTreeMenuDemoView));
  62. AddDocTemplate(pDocTemplate);
  63. // create main MDI Frame window
  64. CMainFrame* pMainFrame = new CMainFrame;
  65. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  66. return FALSE;
  67. m_pMainWnd = pMainFrame;
  68. // Parse command line for standard shell commands, DDE, file open
  69. CCommandLineInfo cmdInfo;
  70. ParseCommandLine(cmdInfo);
  71. // Dispatch commands specified on the command line
  72. if (!ProcessShellCommand(cmdInfo))
  73. return FALSE;
  74. // The main window has been initialized, so show and update it.
  75. pMainFrame->ShowWindow(m_nCmdShow);
  76. pMainFrame->UpdateWindow();
  77. return TRUE;
  78. }
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CAboutDlg
  81. #define NUM_ITEMS 9
  82. #define NUM_SUB 3
  83. static _TCHAR *_gszItem[NUM_ITEMS] = 
  84. {
  85. _T("Fifi"), _T("Babs Bunny"), _T("Buster Bunny"),
  86. _T("Concorde"), _T("Cyote"), _T("Dizzy Devil"), 
  87. _T("Elmyra"), _T("Hampton"), _T("Plucky Duck")
  88. };
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CAboutDlg dialog used for App About
  91. class CAboutDlg : public CDialog
  92. {
  93. public:
  94. CAboutDlg();
  95. // Dialog Data
  96. //{{AFX_DATA(CAboutDlg)
  97. enum { IDD = IDD_ABOUTBOX };
  98. //}}AFX_DATA
  99. // ClassWizard generated virtual function overrides
  100. //{{AFX_VIRTUAL(CAboutDlg)
  101. protected:
  102. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  103. //}}AFX_VIRTUAL
  104. // Implementation
  105. protected:
  106. CTreeMenuFrame m_TreeMenu;
  107. CImageList m_ImageList;
  108. //{{AFX_MSG(CAboutDlg)
  109. virtual BOOL OnInitDialog();
  110. afx_msg void OnButtonShow();
  111. //}}AFX_MSG
  112. afx_msg void OnHide();
  113. DECLARE_MESSAGE_MAP()
  114. };
  115. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  116. {
  117. //{{AFX_DATA_INIT(CAboutDlg)
  118. //}}AFX_DATA_INIT
  119. }
  120. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  121. {
  122. CDialog::DoDataExchange(pDX);
  123. //{{AFX_DATA_MAP(CAboutDlg)
  124. //}}AFX_DATA_MAP
  125. }
  126. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  127. //{{AFX_MSG_MAP(CAboutDlg)
  128. ON_BN_CLICKED(IDC_BUTTON_SHOW, OnButtonShow)
  129. //}}AFX_MSG_MAP
  130. ON_MESSAGE(TMFN_HIDE, OnHide)
  131. END_MESSAGE_MAP()
  132. // App command to run the dialog
  133. void CTreeMenuDemoApp::OnAppAbout()
  134. {
  135. CAboutDlg aboutDlg;
  136. aboutDlg.DoModal();
  137. }
  138. /////////////////////////////////////////////////////////////////////////////
  139. // CTreeMenuDemoApp commands
  140. BOOL CAboutDlg::OnInitDialog() 
  141. {
  142. CDialog::OnInitDialog();
  143. m_TreeMenu.SubclassDlgItem(IDC_CUSTOM, this);
  144. // set appearence params
  145.     m_TreeMenu.m_tree.SetItemHeight(35);
  146. m_TreeMenu.m_tree.SetBorder(TVSBF_XBORDER|TVSBF_YBORDER, 2, 0);
  147. m_TreeMenu.m_tree.SetBkColor(GetSysColor (COLOR_3DDKSHADOW));
  148. m_TreeMenu.m_tree.SetTextColor(GetSysColor (COLOR_3DHILIGHT));
  149. m_TreeMenu.m_tree.SetSelBkColor(RGB(0,0,255));
  150. m_TreeMenu.m_tree.SetSelTextColor(RGB(0,255,255));
  151. // ImageList
  152. m_ImageList.Create(IDB_TINYTOON, 32, 1, RGB(255,0,255));
  153.     
  154.     // Attach ImageList to TreeView
  155.     if (m_ImageList)
  156.         m_TreeMenu.m_tree.SetImageList((HIMAGELIST)m_ImageList,TVSIL_NORMAL);
  157. // Fill TreeView with some stuff...
  158. TVITEM tvi;
  159. TVINSERTSTRUCT tvis;
  160. HTREEITEM hPrev;
  161. tvi.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_CHILDREN;
  162. tvi.hItem = NULL;
  163. tvi.cchTextMax = 64;     
  164. tvi.pszText = _T("Tiny Toons");
  165. tvi.iImage = 0;
  166. tvi.iSelectedImage = 0;
  167. tvis.hParent = TVI_ROOT;
  168. tvis.hInsertAfter = TVI_LAST;
  169. tvis.item = tvi;
  170. hPrev = m_TreeMenu.GetTreeMenuCtrl()->InsertItem(&tvis);
  171. int i;
  172. for(i = 0; i < NUM_SUB; i++)
  173. {
  174. tvi.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  175. tvi.cchTextMax = 64; 
  176. tvi.pszText = _gszItem[i];
  177. tvi.iImage = i+1;
  178. tvi.iSelectedImage = i+1;
  179. tvis.hParent = hPrev;
  180. tvis.hInsertAfter = TVI_LAST;
  181. tvis.item = tvi;
  182. m_TreeMenu.m_tree.InsertItem(&tvis);
  183. }
  184. tvi.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_CHILDREN;
  185. tvi.hItem = NULL;
  186. tvi.pszText = _T("Tiny Toons 2");
  187. tvi.iImage = 0;
  188. tvi.iSelectedImage = 0;
  189. tvis.hParent = TVI_ROOT;
  190. tvis.hInsertAfter = TVI_LAST;
  191. tvis.item = tvi;
  192. hPrev = m_TreeMenu.GetTreeMenuCtrl()->InsertItem(&tvis);
  193. for(i = 0; i < NUM_SUB; i++)
  194. {
  195. tvi.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  196. tvi.pszText = _gszItem[3+i];
  197. tvi.iImage = i+4;
  198. tvi.iSelectedImage = i+4;
  199. tvis.hParent = hPrev;
  200. tvis.hInsertAfter = TVI_LAST;
  201. tvis.item = tvi;
  202. m_TreeMenu.GetTreeMenuCtrl()->InsertItem(&tvis);
  203. }
  204. tvi.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_CHILDREN;
  205. tvi.hItem = NULL;
  206. tvi.pszText = _T("Tiny Toons 3");
  207. tvi.iImage = 0;
  208. tvi.iSelectedImage = 0;
  209. tvis.hParent = TVI_ROOT;
  210. tvis.hInsertAfter = TVI_LAST;
  211. tvis.item = tvi;
  212. hPrev = m_TreeMenu.GetTreeMenuCtrl()->InsertItem(&tvis);
  213. for(i = 0; i < NUM_SUB; i++)
  214. {
  215. tvi.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  216. tvi.pszText = _gszItem[6+i];
  217. tvi.iImage = i+7;
  218. tvi.iSelectedImage = i+7;
  219. tvis.hParent = hPrev;
  220. tvis.hInsertAfter = TVI_LAST;
  221. tvis.item = tvi;
  222. m_TreeMenu.GetTreeMenuCtrl()->InsertItem(&tvis);
  223. }
  224. CString s;
  225. for(i = 0; i < NUM_SUB*2; i++)
  226. {
  227. s.Format("Bookmark No: %i", i+1);
  228. tvi.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  229. tvi.pszText = s.GetBuffer(64);
  230. tvi.hItem = NULL;
  231. tvi.iImage = i+1;
  232. tvi.iSelectedImage = i+4;
  233. tvis.hParent = TVI_ROOT;
  234. tvis.hInsertAfter = TVI_LAST;
  235. tvis.item = tvi;
  236. m_TreeMenu.GetTreeMenuCtrl()->InsertItem(&tvis);
  237. s.ReleaseBuffer();
  238. }
  239. m_TreeMenu.ShowFrameBevel();
  240. m_TreeMenu.CalcLayout();
  241. return TRUE;  // return TRUE unless you set the focus to a control
  242.               // EXCEPTION: OCX Property Pages should return FALSE
  243. }
  244. void CAboutDlg::OnHide() 
  245. {
  246. m_TreeMenu.ShowWindow(SW_HIDE);
  247. }
  248. void CAboutDlg::OnButtonShow() 
  249. {
  250. m_TreeMenu.ShowWindow(SW_SHOW);
  251. }