📄 directorytreectrl.cpp
字号:
{
TRACE(_T("Error Gettting SystemFileInfo!"));
itInsert.itemex.iImage = 0; // :(
}
else
{
itInsert.itemex.iImage = shFinfo.iIcon;
DestroyIcon(shFinfo.hIcon);
if (hRoot == NULL && shFinfo.szDisplayName[0] != _T('\0'))
{
STreeItem* pti = new STreeItem;
pti->strPath = strText;
strText = shFinfo.szDisplayName;
itInsert.item.pszText = strText.GetBuffer();
itInsert.item.cchTextMax = strText.GetLength();
itInsert.item.mask |= TVIF_PARAM;
itInsert.item.lParam = (LPARAM)pti;
}
}
if(!SHGetFileInfo(strTemp, 0, &shFinfo, sizeof(shFinfo),
SHGFI_ICON | SHGFI_OPENICON | SHGFI_SMALLICON))
{
TRACE(_T("Error Gettting SystemFileInfo!"));
itInsert.itemex.iImage = 0;
}
else
{
itInsert.itemex.iSelectedImage = shFinfo.iIcon;
DestroyIcon(shFinfo.hIcon);
}
}
// END: added by FoRcHa //////////////
HTREEITEM hItem = InsertItem(&itInsert);
if (IsShared(strDir))
SetCheck(hItem);
strText.ReleaseBuffer();
return hItem;
}
CString CDirectoryTreeCtrl::GetFullPath(HTREEITEM hItem)
{
CString strDir;
HTREEITEM hSearchItem = hItem;
while(hSearchItem != NULL)
{
CString strSearchItemDir;
STreeItem* pti = (STreeItem*)GetItemData(hSearchItem);
if (pti)
strSearchItemDir = pti->strPath;
else
strSearchItemDir = GetItemText(hSearchItem);
strDir = strSearchItemDir + _T("\\") + strDir;
hSearchItem = GetParentItem(hSearchItem);
}
return strDir;
}
void CDirectoryTreeCtrl::AddSubdirectories(HTREEITEM hRoot, CString strDir)
{
if (strDir.Right(1) != _T("\\"))
strDir += _T("\\");
CFileFind finder;
BOOL bWorking = finder.FindFile(strDir+_T("*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
continue;
if (finder.IsSystem())
continue;
if (!finder.IsDirectory())
continue;
CString strFilename = finder.GetFileName();
if (strFilename.ReverseFind(_T('\\')) != -1)
strFilename = strFilename.Mid(strFilename.ReverseFind(_T('\\')) + 1);
AddChildItem(hRoot, strFilename);
}
finder.Close();
}
bool CDirectoryTreeCtrl::HasSubdirectories(CString strDir)
{
if (strDir.Right(1) != _T('\\'))
strDir += _T('\\');
CFileFind finder;
BOOL bWorking = finder.FindFile(strDir+_T("*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
continue;
if (finder.IsSystem())
continue;
if (!finder.IsDirectory())
continue;
finder.Close();
return true;
}
finder.Close();
return false;
}
void CDirectoryTreeCtrl::GetSharedDirectories(CStringList* list)
{
for (POSITION pos = m_lstShared.GetHeadPosition(); pos != NULL; )
list->AddTail(m_lstShared.GetNext(pos));
}
void CDirectoryTreeCtrl::SetSharedDirectories(CStringList* list)
{
m_lstShared.RemoveAll();
for (POSITION pos = list->GetHeadPosition(); pos != NULL; )
{
CString str = list->GetNext(pos);
if (str.Left(2)==_T("\\\\")) continue;
if (str.Right(1) != _T('\\'))
str += _T('\\');
m_lstShared.AddTail(str);
}
Init();
}
bool CDirectoryTreeCtrl::HasSharedSubdirectory(CString strDir)
{
if (strDir.Right(1) != _T('\\'))
strDir += _T('\\');
strDir.MakeLower();
for (POSITION pos = m_lstShared.GetHeadPosition(); pos != NULL; )
{
CString str = m_lstShared.GetNext(pos);
str.MakeLower();
if (str.Find(strDir) == 0 && strDir != str)//strDir.GetLength() != str.GetLength())
return true;
}
return false;
}
void CDirectoryTreeCtrl::CheckChanged(HTREEITEM hItem, bool bChecked)
{
CString strDir = GetFullPath(hItem);
if (bChecked)
AddShare(strDir);
else
DelShare(strDir);
UpdateParentItems(hItem);
GetParent()->SendMessage(WM_COMMAND, USRMSG_ITEMSTATECHANGED, (long)m_hWnd);
}
bool CDirectoryTreeCtrl::IsShared(CString strDir)
{
if (strDir.Right(1) != _T('\\'))
strDir += _T('\\');
for (POSITION pos = m_lstShared.GetHeadPosition(); pos != NULL; )
{
CString str = m_lstShared.GetNext(pos);
if (str.Right(1) != _T('\\'))
str += _T('\\');
if (str.CompareNoCase(strDir) == 0)
return true;
}
return false;
}
void CDirectoryTreeCtrl::AddShare(CString strDir)
{
if (strDir.Right(1) != _T('\\'))
strDir += _T('\\');
if (IsShared(strDir) || !strDir.CompareNoCase(CString(thePrefs.GetConfigDir()) ))
return;
m_lstShared.AddTail(strDir);
}
void CDirectoryTreeCtrl::DelShare(CString strDir)
{
if (strDir.Right(1) != _T('\\'))
strDir += _T('\\');
for (POSITION pos = m_lstShared.GetHeadPosition(); pos != NULL; )
{
POSITION pos2 = pos;
CString str = m_lstShared.GetNext(pos);
if (str.CompareNoCase(strDir) == 0)
m_lstShared.RemoveAt(pos2);
}
}
void CDirectoryTreeCtrl::UpdateParentItems(HTREEITEM hChild)
{
HTREEITEM hSearch = GetParentItem(hChild);
while(hSearch != NULL)
{
if (HasSharedSubdirectory(GetFullPath(hSearch)))
SetItemState(hSearch, TVIS_BOLD, TVIS_BOLD);
else
SetItemState(hSearch, 0, TVIS_BOLD);
hSearch = GetParentItem(hSearch);
}
}
void CDirectoryTreeCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
CPoint ptMenu(-1, -1);
if (point.x != -1 && point.y != -1)
{
ptMenu = point;
ScreenToClient(&point);
}
else
{
HTREEITEM hSel = GetNextItem(TVI_ROOT, TVGN_CARET);
if (hSel)
{
CRect rcItem;
if (GetItemRect(hSel, &rcItem, TRUE))
{
ptMenu.x = rcItem.left;
ptMenu.y = rcItem.top;
ClientToScreen(&ptMenu);
}
}
else
ClientToScreen(&(ptMenu = (0, 0)));
}
HTREEITEM hItem = HitTest(point);
// create the menu
CTitleMenu SharedMenu;
SharedMenu.CreatePopupMenu();
if (m_lstShared.GetCount() == 0)
SharedMenu.AddMenuTitle(GetResString(IDS_NOSHAREDFOLDERS));
else
SharedMenu.AddMenuTitle(GetResString(IDS_SHAREDFOLDERS));
// add right clicked folder, if any
if (hItem)
{
m_strLastRightClicked = GetFullPath(hItem);
if (!IsShared(m_strLastRightClicked))
SharedMenu.AppendMenu(MF_STRING, MP_SHAREDFOLDERS_FIRST-1, (LPCTSTR)(GetResString(IDS_VIEW1) + m_strLastRightClicked +GetResString(IDS_VIEW2)));
}
// add all shared directories
int iCnt = 0;
for (POSITION pos = m_lstShared.GetHeadPosition(); pos != NULL; iCnt++)
SharedMenu.AppendMenu(MF_STRING,MP_SHAREDFOLDERS_FIRST+iCnt, (LPCTSTR)m_lstShared.GetNext(pos));
// display menu
SharedMenu.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, ptMenu.x, ptMenu.y, this);
VERIFY( SharedMenu.DestroyMenu() );
}
void CDirectoryTreeCtrl::OnRButtonDown(UINT nFlags, CPoint point)
{
// catch WM_RBUTTONDOWN and do not route it the default way.. otherwise we won't get a WM_CONTEXTMENU.
//CTreeCtrl::OnRButtonDown(nFlags, point);
}
BOOL CDirectoryTreeCtrl::OnCommand(WPARAM wParam,LPARAM lParam ){
if (wParam < MP_SHAREDFOLDERS_FIRST)
{
ShellExecute(NULL, _T("open"), m_strLastRightClicked, NULL, NULL, SW_SHOW);
return false;
}
int cnt = 0;
for (POSITION pos = m_lstShared.GetHeadPosition(); pos != NULL; )
{
CString str = m_lstShared.GetNext(pos);
if (cnt == wParam-MP_SHAREDFOLDERS_FIRST)
{
ShellExecute(NULL, _T("open"), str, NULL, NULL, SW_SHOW);
return true;
}
cnt++;
}
return true;
}
void CDirectoryTreeCtrl::OnTvnDeleteItem(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
if (pNMTreeView->itemOld.lParam)
delete (STreeItem*)pNMTreeView->itemOld.lParam;
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -