?? mytreectrl.cpp
字號:
// MyTreeCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "MFCFatline.h"
#include "MyTreeCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyTreeCtrl
CMyTreeCtrl::CMyTreeCtrl()
{
FATBuffer=new BYTE[512*8+1];
FDTBuffer=new BYTE[512*8+1];
}
CMyTreeCtrl::~CMyTreeCtrl()
{
delete[]FATBuffer;
delete[]FDTBuffer;
}
BEGIN_MESSAGE_MAP(CMyTreeCtrl, CTreeCtrl)
//{{AFX_MSG_MAP(CMyTreeCtrl)
ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyTreeCtrl message handlers
void CMyTreeCtrl::Init()
{
DWORD dwStyle = GetWindowLong(m_hWnd,GWL_STYLE);
dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT;
SetWindowLong(m_hWnd,GWL_STYLE,dwStyle);
m_hRoot = InsertItem("我的電腦");
GetLogicalDrives(m_hRoot);
GetDriveDir(m_hRoot);
Expand(m_hRoot,TVE_EXPAND);
}
//////////////////////////////////////////////////////////////////////////
////函數功能:獲取驅動器
//////////////////////////////////////////////////////////////////////////
void CMyTreeCtrl::GetLogicalDrives(HTREEITEM hParent)
{
size_t szAllDriveStrings = GetLogicalDriveStrings(0,NULL);//獲得要存放驅動器名buffer的大小
char *pDriveStrings = new char[szAllDriveStrings + sizeof(_T(""))];//初始化buffer,結尾以0結尾
GetLogicalDriveStrings(szAllDriveStrings,pDriveStrings);//獲取驅動器名稱
size_t szDriveString = strlen(pDriveStrings);//獲取每個驅動器名稱所占的大小
while(szDriveString > 0)
{
if(GetDriveType(pDriveStrings)== DRIVE_CDROM)
{
pDriveStrings += szDriveString + 1;
szDriveString = strlen(pDriveStrings);
continue;
}
InsertItem(pDriveStrings,hParent,TVI_LAST);//將驅動器的名字插入到樹控件
pDriveStrings += szDriveString + 1;//移動指針到下一個驅動器名字
szDriveString = strlen(pDriveStrings);//獲取下一個驅動器名的大小
}
}
/////////////////////////////////////////////////////////////
//獲取驅動器目錄下的子項
//////////////////////////////////////////////////////////////////////////
void CMyTreeCtrl::GetDriveDir(HTREEITEM hParent)
{
HTREEITEM hChild =GetChildItem(hParent);
while(hChild)
{
CString strText =GetItemText(hChild);//獲得樹空間中第一個分區的文本內容
if(strText.Right(1) != "\\")//判斷分區名稱的后面是否有“\”
strText += _T("\\");//如果沒有“\”,添加
strText += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(strText);
while(bContinue)
{
bContinue = file.FindNextFile();
if( !file.IsDots())
InsertItem(file.GetFileName(),hChild);
}
GetDriveDir(hChild);
hChild = GetNextItem(hChild,TVGN_NEXT);
}
}
///////////////////////////////////////////////////////////////////////////////
//單擊文件 顯示磁盤中對應數據區
//單擊目錄 顯示磁盤中對應的目錄內的內容(即該目錄下的FDT)
//單擊分區 顯示該分區的DBR,同時修改CMFCFATLineDlg中的comboBox和m_biaoti內容
//以上內容都顯示在 CMFCFATLineDlg中的 edit中
//////////////////////////////////////////////////////////////////////////////
void CMyTreeCtrl::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
/* NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TVITEM item = pNMTreeView->itemNew;
if(item.hItem == m_hRoot)//如果是根目錄
// if(item.hItem==)
CString str = GetFullPath(item.hItem);
if(str.Right(1) != "\\")
str += "\\";
str += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(str);
while(bContinue)
{
bContinue = file.FindNextFile();
if(!file.IsDirectory() && !file.IsDots())
{
SHFILEINFO info;
CString temp = str;
int index = temp.Find("*.*");
temp.Delete(index,3);
SHGetFileInfo(temp + file.GetFileName(),0,&info,sizeof(&info),SHGFI_DISPLAYNAME | SHGFI_ICON);
int i = m_ImageList.Add(info.hIcon);
m_list.InsertItem(i,info.szDisplayName,i);
}
}
*/
*pResult = 0;
}
//////////////////////////////////////////////////////////////////////////
////函數功能:展開事件函數
//////////////////////////////////////////////////////////////////////////
void CMyTreeCtrl::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TVITEM item = pNMTreeView->itemNew;
if(item.hItem == m_hRoot)
return;
HTREEITEM hChild = GetChildItem(item.hItem);
while(hChild)
{
AddSubDir(hChild);
hChild = GetNextItem(hChild,TVGN_NEXT);
}
*pResult = 0;
}
//////////////////////////////////////////////////////////////////////////
//函數功能:獲取樹項目全跟徑
//////////////////////////////////////////////////////////////////////////
CString CMyTreeCtrl::GetFullPath(HTREEITEM hCurrent)
{
CString strTemp;
CString strReturn = "";
while(hCurrent != m_hRoot)
{
strTemp = GetItemText(hCurrent);
if(strTemp.Right(1) != "\\")
strTemp += "\\";
strReturn = strTemp + strReturn;
hCurrent = GetParentItem(hCurrent);
}
return strReturn;
}
//////////////////////////////////////////////////////////////////////////
//添加子目錄
//////////////////////////////////////////////////////////////////////////
void CMyTreeCtrl::AddSubDir(HTREEITEM hParent)
{
CString strPath = GetFullPath(hParent);
if(strPath.Right(1) != "\\")
strPath += "\\";
strPath += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(strPath);
while(bContinue)
{
bContinue = file.FindNextFile();
//if(file.IsDirectory() && !file.IsDots())
if(!file.IsDots())
InsertItem(file.GetFileName(),hParent);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -