DirTreeCtrl--显示文件夹和文件
作者:Michael Hofer 编译:刘峰
环境:Visual C++ 6.0 SP3
DirTreeCtrl允许用户显示文件夹和文件名(可选)。该控制使用系统的图象列表
显示每项的图标。
你不必管理图标资源。项排序首先通过文件夹,然后通过文件名(如果选择了)。
你可以获得每项的全路径名;
你可以设置它的其始路径;
你可以选择一个子路径。
在对话框里使用该控制:
1、在对话框资源加入树控制
2、在对话框头文件定义成员变量
CDirTreeCtrl m_DirTree;
3、在InitDialog中加入下列代码:
BOOL CTreeDialog::OnInitDialog()
{
CDialog::OnInitDialog();
TCHAR szWorkDir[MAX_PATH];
// TODO: Add extra initialization here
// Here we subclass our CDirTreeCtrl
if ( !m_TreeCtrl.m_hWnd )
{
if ( m_TreeCtrl.SubclassDlgItem( IDC_TREE1, this ) )
{
m_TreeCtrl.DisplayTree( NULL /*Display all*/,
TRUE /* TRUE = Display Files*/ );
_getcwd( szWorkDir, 256 );
// set the Path to the current Work-Directory
m_TreeCtrl.SetSelPath( szWorkDir );
}
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
在视图类里使用该控制:
在视图的头文件中定义该控制的成员变量和标识符ID_TREECTRL;
修改视图类的Create函数;
修改视图类的OnSize函数。
BOOL CLeftView::Create(LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID,
CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bRet;
bRet = CWnd::Create(lpszClassName,
lpszWindowName,
dwStyle,
rect,
pParentWnd,
nID,
pContext);
// this part creates the TreeCtrl and use the CLeftView
// as his parent Window
if ( m_DirTree.m_hWnd == NULL && bRet )
{
bRet = m_DirTree.Create(WS_CHILD | TVS_LINESATROOT |
TVS_HASBUTTONS | WS_VISIBLE |
TVS_HASLINES,
CRect(0, 0, 0, 0),
this
ID_TREECTRL );
if ( bRet )
m_DirTree.DisplayTree( NULL, TRUE );
}
return bRet;
}
void CLeftView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if ( m_DirTree.m_hWnd )
m_DirTree.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE );
}
作者:Michael Hofer 编译:刘峰
环境:Visual C++ 6.0 SP3
DirTreeCtrl允许用户显示文件夹和文件名(可选)。该控制使用系统的图象列表
显示每项的图标。
你不必管理图标资源。项排序首先通过文件夹,然后通过文件名(如果选择了)。
你可以获得每项的全路径名;
你可以设置它的其始路径;
你可以选择一个子路径。
在对话框里使用该控制:
1、在对话框资源加入树控制
2、在对话框头文件定义成员变量
CDirTreeCtrl m_DirTree;
3、在InitDialog中加入下列代码:
BOOL CTreeDialog::OnInitDialog()
{
CDialog::OnInitDialog();
TCHAR szWorkDir[MAX_PATH];
// TODO: Add extra initialization here
// Here we subclass our CDirTreeCtrl
if ( !m_TreeCtrl.m_hWnd )
{
if ( m_TreeCtrl.SubclassDlgItem( IDC_TREE1, this ) )
{
m_TreeCtrl.DisplayTree( NULL /*Display all*/,
TRUE /* TRUE = Display Files*/ );
_getcwd( szWorkDir, 256 );
// set the Path to the current Work-Directory
m_TreeCtrl.SetSelPath( szWorkDir );
}
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
在视图类里使用该控制:
在视图的头文件中定义该控制的成员变量和标识符ID_TREECTRL;
修改视图类的Create函数;
修改视图类的OnSize函数。
BOOL CLeftView::Create(LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID,
CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bRet;
bRet = CWnd::Create(lpszClassName,
lpszWindowName,
dwStyle,
rect,
pParentWnd,
nID,
pContext);
// this part creates the TreeCtrl and use the CLeftView
// as his parent Window
if ( m_DirTree.m_hWnd == NULL && bRet )
{
bRet = m_DirTree.Create(WS_CHILD | TVS_LINESATROOT |
TVS_HASBUTTONS | WS_VISIBLE |
TVS_HASLINES,
CRect(0, 0, 0, 0),
this
ID_TREECTRL );
if ( bRet )
m_DirTree.DisplayTree( NULL, TRUE );
}
return bRet;
}
void CLeftView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if ( m_DirTree.m_hWnd )
m_DirTree.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE );
}