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

对话框与窗口

开发平台:

Visual C++

  1. // TreeCtrlDlg.cpp : implementation file
  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 "TreeCtrl.h"
  22. #include "TreeCtrlDlg.h"
  23. #include "AboutDlg.h"
  24. #include "PropertiesDlg.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CTreeCtrlDlg dialog
  32. CTreeCtrlDlg::CTreeCtrlDlg(CWnd* pParent /*=NULL*/)
  33. : CXTResizeDialog(CTreeCtrlDlg::IDD, pParent), m_pTreeFocus(NULL)
  34. {
  35. //{{AFX_DATA_INIT(CTreeCtrlDlg)
  36. //}}AFX_DATA_INIT
  37. // Enable Ofice XP themes
  38. XTThemeManager()->SetTheme(xtThemeOfficeXP);
  39. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  40. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  41. }
  42. void CTreeCtrlDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CXTResizeDialog::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CTreeCtrlDlg)
  46. DDX_Control(pDX, IDC_BTN_PROPERTIES, m_btnProperties);
  47. DDX_Control(pDX, IDC_TREE_STATE, m_treeState);
  48. DDX_Control(pDX, IDC_TREE_SELECT, m_treeSelect);
  49. //}}AFX_DATA_MAP
  50. }
  51. IMPLEMENT_DYNAMIC(CTreeCtrlDlg, CXTResizeDialog)
  52. BEGIN_MESSAGE_MAP(CTreeCtrlDlg, CXTResizeDialog)
  53. //{{AFX_MSG_MAP(CTreeCtrlDlg)
  54. ON_WM_SYSCOMMAND()
  55. ON_WM_PAINT()
  56. ON_WM_QUERYDRAGICON()
  57. ON_NOTIFY(NM_RCLICK, IDC_TREE_SELECT, OnRclickTreeCtrl)
  58. ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  59. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  60. ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  61. ON_NOTIFY(TVN_ENDLABELEDIT, IDC_TREE_SELECT, OnEndlabeleditTreeCtrl)
  62. ON_WM_DESTROY()
  63. ON_BN_CLICKED(IDC_BTN_PROPERTIES, OnBtnProperties)
  64. //}}AFX_MSG_MAP
  65. END_MESSAGE_MAP()
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CTreeCtrlDlg message handlers
  68. BOOL CTreeCtrlDlg::OnInitDialog()
  69. {
  70. CXTResizeDialog::OnInitDialog();
  71. // Add "About..." menu item to system menu.
  72. // IDM_ABOUTBOX must be in the system command range.
  73. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  74. ASSERT(IDM_ABOUTBOX < 0xF000);
  75. CMenu* pSysMenu = GetSystemMenu(FALSE);
  76. if (pSysMenu != NULL)
  77. {
  78. CString strAboutMenu;
  79. strAboutMenu.LoadString(IDS_ABOUTBOX);
  80. if (!strAboutMenu.IsEmpty())
  81. {
  82. pSysMenu->AppendMenu(MF_SEPARATOR);
  83. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  84. }
  85. }
  86. // Set the icon for this dialog.  The framework does this automatically
  87. //  when the application's main window is not a dialog
  88. SetIcon(m_hIcon, TRUE);         // Set big icon
  89. SetIcon(m_hIcon, FALSE);        // Set small icon
  90. // Set control resizing.
  91. SetResize(IDC_TREE_SELECT,    SZ_TOP_LEFT,     SZ_BOTTOM_CENTER);
  92. SetResize(IDC_TREE_STATE,     SZ_TOP_CENTER,   SZ_BOTTOM_RIGHT);
  93. SetResize(IDC_TXT_STATE,      SZ_TOP_CENTER,   SZ_TOP_CENTER);
  94. SetResize(IDC_STATIC_SEP,     SZ_BOTTOM_LEFT,  SZ_BOTTOM_RIGHT);
  95. SetResize(IDC_BTN_PROPERTIES, SZ_BOTTOM_RIGHT, SZ_BOTTOM_RIGHT);
  96. SetResize(IDCANCEL,           SZ_BOTTOM_RIGHT, SZ_BOTTOM_RIGHT);
  97. // Load window placement
  98. LoadPlacement(_T("CTreeCtrlDlg"));
  99. m_treeSelect.EnableMultiSelect();
  100. HTREEITEM hItemSelect = m_treeSelect.InsertItem(_T("Root Item"), 0, 1);
  101. m_treeSelect.SetItemBold(hItemSelect);
  102. m_treeSelect.FocusItem(hItemSelect);
  103. HTREEITEM hItemState = m_treeState.InsertItemState(_T("Root Item"), 0, 1);
  104. m_treeState.SetItemBold(hItemState);
  105. int i, nState = STATE_UNCHECKED;
  106. for(i = 0; i < 10; ++i )
  107. {
  108. m_treeSelect.InsertItem(_T("Child Item"), 0, 1, hItemSelect);
  109. m_treeState.InsertItemState(_T("Child Item"), 0, 1, nState, hItemState);
  110. ++nState;
  111. if (nState > STATE_INTERMEDIATE)
  112. nState = STATE_UNCHECKED;
  113. }
  114. m_treeSelect.Expand(hItemSelect, TVE_EXPAND);
  115. m_treeSelect.SetFont(&XTAuxData().font);
  116. m_treeState.Expand(hItemState, TVE_EXPAND);
  117. m_treeState.SetFont(&XTAuxData().font);
  118. EnableProperties();
  119. return TRUE;  // return TRUE  unless you set the focus to a control
  120. }
  121. void CTreeCtrlDlg::OnDestroy()
  122. {
  123. // Save window placement
  124. SavePlacement(_T("CTreeCtrlDlg"));
  125. CXTResizeDialog::OnDestroy();
  126. }
  127. void CTreeCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  128. {
  129. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  130. {
  131. CAboutDlg dlgAbout;
  132. dlgAbout.DoModal();
  133. }
  134. else
  135. {
  136. CXTResizeDialog::OnSysCommand(nID, lParam);
  137. }
  138. }
  139. // If you add a minimize button to your dialog, you will need the code below
  140. //  to draw the icon.  For MFC applications using the document/view model,
  141. //  this is automatically done for you by the framework.
  142. void CTreeCtrlDlg::OnPaint()
  143. {
  144. if (IsIconic())
  145. {
  146. CPaintDC dc(this); // device context for painting
  147. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  148. // Center icon in client rectangle
  149. int cxIcon = GetSystemMetrics(SM_CXICON);
  150. int cyIcon = GetSystemMetrics(SM_CYICON);
  151. CRect rect;
  152. GetClientRect(&rect);
  153. int x = (rect.Width() - cxIcon + 1) / 2;
  154. int y = (rect.Height() - cyIcon + 1) / 2;
  155. // Draw the icon
  156. dc.DrawIcon(x, y, m_hIcon);
  157. }
  158. else
  159. {
  160. CXTResizeDialog::OnPaint();
  161. }
  162. }
  163. // The system calls this to obtain the cursor to display while the user drags
  164. //  the minimized window.
  165. HCURSOR CTreeCtrlDlg::OnQueryDragIcon()
  166. {
  167. return (HCURSOR) m_hIcon;
  168. }
  169. void CTreeCtrlDlg::OnRclickTreeCtrl(NMHDR* /*pNMHDR*/, LRESULT* pResult)
  170. {
  171. CPoint point;
  172. GetCursorPos(&point);
  173. CMenu menu;
  174. VERIFY(menu.LoadMenu(IDR_POPUP_TREE));
  175. CMenu* pPopup = menu.GetSubMenu(0);
  176. ASSERT(pPopup != NULL);
  177. CWnd* pWndPopupOwner = this;
  178. while (pWndPopupOwner->GetStyle() & WS_CHILD)
  179. pWndPopupOwner = pWndPopupOwner->GetParent();
  180. pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
  181. pWndPopupOwner);
  182. *pResult = 0;
  183. }
  184. void CTreeCtrlDlg::OnEditCut()
  185. {
  186. // TODO: Add your command handler code here
  187. AfxMessageBox(_T("You selected Cut!"));
  188. }
  189. void CTreeCtrlDlg::OnEditCopy()
  190. {
  191. // TODO: Add your command handler code here
  192. AfxMessageBox(_T("You selected Copy!"));
  193. }
  194. void CTreeCtrlDlg::OnEditPaste()
  195. {
  196. // TODO: Add your command handler code here
  197. AfxMessageBox(_T("You selected Paste!"));
  198. }
  199. void CTreeCtrlDlg::OnEndlabeleditTreeCtrl(NMHDR* pNMHDR, LRESULT* pResult)
  200. {
  201. TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
  202. // TODO: Add your control notification handler code here
  203. if (pTVDispInfo->item.pszText)
  204. {
  205. m_treeSelect.SetItemText(pTVDispInfo->item.hItem,
  206. pTVDispInfo->item.pszText);
  207. }
  208. *pResult = 0;
  209. }
  210. BOOL CTreeCtrlDlg::PreTranslateMessage(MSG* pMsg)
  211. {
  212. if (pMsg->wParam == VK_RETURN ||
  213. pMsg->wParam == VK_ESCAPE)
  214. {
  215. // don't allow dialog to process return or escape keys.
  216. ::TranslateMessage(pMsg);
  217. ::DispatchMessage(pMsg);
  218. return TRUE;
  219. }
  220. return CXTResizeDialog::PreTranslateMessage(pMsg);
  221. }
  222. void CTreeCtrlDlg::SetTreeFocus(CXTTreeCtrl* pTreeFocus)
  223. {
  224. ASSERT_VALID(pTreeFocus);
  225. m_pTreeFocus = pTreeFocus;
  226. m_treeState.SendMessage(WM_NCPAINT);
  227. m_treeSelect.SendMessage(WM_NCPAINT);
  228. EnableProperties();
  229. }
  230. void CTreeCtrlDlg::EnableProperties()
  231. {
  232. if (m_pTreeFocus == &m_treeState) {
  233. m_btnProperties.EnableWindow(m_treeState.GetSelectedItem() != NULL);
  234. }
  235. else if (m_pTreeFocus == &m_treeSelect) {
  236. m_btnProperties.EnableWindow(m_treeSelect.GetFirstSelectedItem() != NULL);
  237. }
  238. else {
  239. m_btnProperties.EnableWindow(FALSE);
  240. }
  241. }
  242. void CTreeCtrlDlg::OnBtnProperties() 
  243. {
  244. m_dlg.DoModalEx(m_pTreeFocus);
  245. }