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

对话框与窗口

开发平台:

Visual C++

  1. // HisTreeCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "HisTreeCtrl.h"
  5. #include "HistoryTree.h"
  6. #include "MainFrm.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. ////////////////////////////////////////////////////////////////////////////
  13. // CHisTreeCtrl
  14. CHisTreeCtrl::CHisTreeCtrl()
  15. {
  16. }
  17. CHisTreeCtrl::~CHisTreeCtrl()
  18. {
  19. m_astrHist.RemoveAll();
  20. }
  21. BEGIN_MESSAGE_MAP(CHisTreeCtrl, CTreeCtrl)
  22. //{{AFX_MSG_MAP(CHisTreeCtrl)
  23. ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
  24. ON_WM_MOUSEMOVE()
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CHisTreeCtrl message handlers
  29. void CHisTreeCtrl::OnClick(NMHDR* /*pNMHDR*/, LRESULT* pResult) 
  30. {
  31. // TODO: Add your control notification handler code here
  32. HTREEITEM hItem;
  33. CPoint pt;
  34. try{
  35. GetCursorPos(&pt);
  36. ScreenToClient(&pt);
  37. hItem = HitTest(pt);
  38. if ((hItem != NULL) && !ItemHasChildren(hItem))
  39. {
  40. int nImg, nOImg;
  41. GetItemImage(hItem, nImg, nOImg);
  42. if(nImg==2)
  43. {
  44. OpenWindow(m_astrHist[GetItemData(hItem)-1]);
  45. }
  46. else
  47. {
  48. //get folder
  49. LPITEMIDLIST pidlHistory = NULL;
  50.    HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_HISTORY, &pidlHistory);
  51.    if (NOERROR != hr)
  52.   return;
  53.    IShellFolder* psfDesktop;
  54.    IShellFolder* psfHistory;
  55.    hr = SHGetDesktopFolder(&psfDesktop);
  56.    if (NOERROR != hr)
  57.   return;
  58.    hr = psfDesktop->BindToObject(pidlHistory, NULL, IID_IShellFolder,
  59.  (LPVOID*)&psfHistory);
  60.    psfDesktop->Release();
  61.    if (NOERROR != hr)
  62.   return;
  63.    HTREEITEM hDayItem;
  64.    hDayItem = GetParentItem(hItem);
  65.    if(hDayItem == NULL)
  66.    hDayItem = hItem;
  67. CString folder = GetItemText(hDayItem);
  68. LPITEMIDLIST pidlSubFolder = FindFolder(folder, psfHistory);
  69. if(pidlSubFolder!=NULL)
  70. {
  71. IShellFolder* psfSubFolder = NULL;
  72. IShellFolder* psfSubFolder2 = NULL;
  73.     hr = psfHistory->BindToObject(pidlSubFolder, NULL, IID_IShellFolder,
  74.  (LPVOID*)&psfSubFolder);
  75. if( hDayItem != hItem)
  76. {
  77. if (pidlSubFolder)
  78. ((CHistoryTree*)m_pHisTree)->m_pMalloc->Free(pidlSubFolder);
  79. folder = GetItemText(hItem);
  80. pidlSubFolder = FindFolder(folder, psfSubFolder);
  81. //psfSubFolder->Release();
  82. hr = psfSubFolder->BindToObject(pidlSubFolder, NULL, IID_IShellFolder,
  83.  (LPVOID*)&psfSubFolder2);
  84. }
  85. if(nImg ==0)
  86. ((CHistoryTree*)m_pHisTree)->AddHistory(psfSubFolder, hItem, 1,3);
  87. else
  88. ((CHistoryTree*)m_pHisTree)->AddHistory(psfSubFolder2, hItem, 2,2);
  89. if(psfSubFolder)
  90. psfSubFolder ->Release();
  91. if(psfSubFolder2)
  92. psfSubFolder2 ->Release();
  93. }
  94. ((CHistoryTree*)m_pHisTree)->m_pMalloc->Free(pidlSubFolder);
  95.    ((CHistoryTree*)m_pHisTree)->m_pMalloc->Free(pidlHistory);
  96.    psfHistory->Release();
  97. Expand(hItem,TVE_TOGGLE);
  98. }
  99. }
  100. else
  101. Expand(hItem,TVE_TOGGLE);
  102. }catch(...)
  103. {
  104. }
  105. *pResult = 0;
  106. }
  107. LPITEMIDLIST CHisTreeCtrl::FindFolder(LPCTSTR foldername, IShellFolder *pFolder)
  108. {
  109. IEnumIDList* pItems = NULL;
  110. LPITEMIDLIST pidlNext = NULL;
  111. CString lpszName;
  112. HRESULT hr = pFolder->EnumObjects(NULL, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS, &pItems);
  113.    while (NOERROR == hr)
  114.    {
  115.   hr = pItems->Next(1, &pidlNext, NULL);
  116.   if (NOERROR == hr)
  117.   {
  118.          m_pHisTree->GetName(pFolder, pidlNext, SHGDN_NORMAL, lpszName);
  119.  if(_tcscmp(foldername, lpszName)==0)
  120.  {
  121.    if (pItems)
  122.   pItems->Release();
  123.  return pidlNext;
  124.  }
  125.  else if (pidlNext)
  126.  {  
  127. ((CHistoryTree*)m_pHisTree)->m_pMalloc->Free(pidlNext);
  128.  }
  129.   }
  130.    }
  131.    if (pItems)
  132.   pItems->Release();
  133.    return NULL;
  134. }
  135. void CHisTreeCtrl::OnMouseMove(UINT nFlags, CPoint point) 
  136. {
  137. CTreeCtrl::OnMouseMove(nFlags, point);
  138. }