📄 pathbox.cpp
字号:
// PathBox.cpp : implementation file
//
#include "stdafx.h"
#include "controls.h"
#include "PathBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPathBox
CPathBox::CPathBox()
{
}
CPathBox::~CPathBox()
{
}
BEGIN_MESSAGE_MAP(CPathBox, CComboBoxEx)
//{{AFX_MSG_MAP(CPathBox)
ON_NOTIFY_REFLECT(CBEN_GETDISPINFO, OnGetdispinfo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPathBox message handlers
BOOL CPathBox::SetPath(LPCTSTR pszPath)
{
CString strPath = pszPath;
InitialUpdate (strPath);
ExpandItem (strPath);
return (TRUE);
}
void CPathBox::InitialUpdate(CString &strPath)
{
if (GetImageList() != NULL)
return;
TCHAR szCurDrive[3];
strncpy (szCurDrive, (LPCSTR) strPath, 2);
szCurDrive [2] = _T('\0');
//
// Get the system image list and attach to it.
//
CControlsApp *app = (CControlsApp *) AfxGetApp();
CImageList *Images = app->GetSystemImageList();
SetImageList (Images);
int nPos = 0;
int nCount = 0;
CString strDrives = _T ("?:\\");
DWORD dwDrives = ::GetLogicalDrives ();
POSITION pos = NULL;
while (dwDrives)
{
if (dwDrives & 1)
{
strDrives.SetAt (0, _T ('A') + nPos);
CString strDrive = strDrives.Left (2);
UINT nType = ::GetDriveType (strDrives);
CString strRoot;
strRoot = strDrive + '\\';
CPathData PathData;
PathData.strName = strDrive;
PathData.bVisible = true;
PathData.nLevel = 0;
PathData.nIndent = 0;
PathData.nImage = GetIconIndex (strRoot, FILE_ATTRIBUTE_SYSTEM);
pos = m_PathData.AddTail (PathData);
}
dwDrives >>= 1;
nPos++;
}
UpdateDisplay (pos);
}
void CPathBox::UpdateDisplay(POSITION &posSel)
{
ResetContent ();
int nCount = 0;
POSITION pos = m_PathData.GetHeadPosition ();
while (pos != NULL)
{
COMBOBOXEXITEM cbei;
cbei.lParam = (LPARAM) pos;
CPathData pb = m_PathData.GetNext (pos);
if (pb.bVisible == false)
continue;
cbei.mask = CBEIF_TEXT | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE | CBEIF_LPARAM | CBEIF_INDENT;
cbei.iItem = nCount++;
cbei.iIndent = pb.nIndent;
cbei.pszText = LPSTR_TEXTCALLBACK;
cbei.iImage = pb.nImage;
cbei.iSelectedImage = cbei.iImage;
int nItem = InsertItem (&cbei);
if ((POSITION) cbei.lParam == posSel)
SetCurSel (nItem);
}
}
void CPathBox::ExpandItem(CString &strPath)
{
TCHAR szCurDrive[3];
strncpy (szCurDrive, (LPCSTR) strPath, 2);
szCurDrive [2] = _T('\0');
int nSel = FindStringExact (-1, szCurDrive);
if (nSel < 0)
return;
SetCurSel (nSel);
POSITION pos = (POSITION) GetItemData (nSel);
POSITION next = pos;
CPathData pf = m_PathData.GetNext (next);
POSITION parent = pf.ParentItem;
CString strFile = strPath;
int nCount = 0;
int nIndex = 0;
while ((nIndex = strPath.Find ('\\', nIndex)) >= 0)
{
++nIndex;
++nCount;
}
++nCount;
CString *strElems = new CString [nCount];
int nStart = 0;
for (nIndex = 0; nIndex < nCount; ++nIndex)
{
int nEnd = strPath.Find ('\\', nStart);
if (nEnd < 0)
{
strElems[nIndex] = strPath.Right (strPath.GetLength () - nStart);
break;
}
strElems [nIndex] = strPath.Mid (nStart, nEnd - nStart);
nStart = nEnd + 1;
}
#ifndef _FULLDIRTREE
CString strItemPath = szCurDrive;
for (nIndex = 1; nIndex < nCount; ++nIndex)
{
strItemPath += '\\';
strItemPath += strElems[nIndex];
CPathData pfNew;
pfNew.strName = strElems[nIndex];
pfNew.ParentItem = next;
pfNew.bVisible = true;
pfNew.nImage = GetIconIndex (strItemPath, FILE_ATTRIBUTE_DIRECTORY);
pfNew.nSelImage = pfNew.nImage;
pfNew.nIndent = 2 * nIndex;
pos = m_PathData.InsertAfter (pos, pfNew);
}
UpdateDisplay (pos);
delete [] strElems;
#else
POSITION nextpos = pos;
POSITION selpos = pos;
CString strLevel = _T("");
for (nIndex = 0; nIndex < nCount; ++nIndex)
{
strLevel += strElems[nIndex] + '\\';
CString strTest = strPath.Left (strLevel.GetLength());
if (nIndex < (nCount - 1))
{
strTest += strElems[nIndex + 1];
}
else
strTest.Empty ();
CString strLevelPath = strLevel + "*.*";
WIN32_FIND_DATA fd;
int nItem = GetCount ();
HANDLE hFind = FindFirstFile ((LPCSTR) strLevelPath, &fd);
if (hFind == NULL)
break;
do
{
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
continue;
if (*(fd.cFileName) == '.')
continue;
CPathData PathData;
PathData.strName = fd.cFileName;
PathData.bVisible = true;
PathData.nLevel = 1;
PathData.ParentItem = parent;
CString strData;
strData.Format ("%s%s", (LPCSTR) strLevel, fd.cFileName);
PathData.nImage = GetIconIndex (strData, FILE_ATTRIBUTE_DIRECTORY);
PathData.nIndent = pf.nIndent + 2;
pos = m_PathData.InsertAfter (next, PathData);
if (!strTest.Compare ((LPCSTR) strData))
{
nextpos = pos;
selpos = pos;
}
next = pos;
} while (FindNextFile (hFind, &fd));
next = nextpos;
pf = m_PathData.GetNext(nextpos);
FindClose (hFind);
}
UpdateDisplay (selpos);
#endif // _FULLDIRTREE
}
POSITION CPathBox::GetSelPosition()
{
int nSel = GetCurSel ();
if (nSel < 0)
return (NULL);
POSITION pos = (POSITION) GetItemData (nSel);
return (pos);
}
int CPathBox::GetIconIndex(CString &strFile, DWORD dwAttributes)
{
SHFILEINFO sfi;
memset(&sfi, 0, sizeof(sfi));
SHGetFileInfo (strFile, dwAttributes,
&sfi, sizeof(sfi),
SHGFI_SMALLICON | SHGFI_SYSICONINDEX |
SHGFI_USEFILEATTRIBUTES);
return (sfi.iIcon);
}
void CPathBox::OnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult)
{
NMCOMBOBOXEX *pData = (NMCOMBOBOXEX *) pNMHDR;
COMBOBOXEXITEM *pItem = &pData->ceItem;
if (pItem->mask & CBEIF_TEXT)
{
CPathData pd = m_PathData.GetAt ((POSITION) pItem->lParam);
::lstrcpy (pItem->pszText, (LPCSTR) pd.strName);
}
*pResult = 0;
}
//
// Constructor for the data class
//
CPathBox::CPathData::CPathData ()
{
strName = _T("");
nItem = 0;
ParentItem = NULL;
nImage = -1;
nSelImage = -1;
dwData = 0;
Expanded = false;
nIndent = 0;
nMode = 0;
nLevel = 0;
bVisible = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -