Click here to Skip to main content
15,891,895 members
Articles / Desktop Programming / MFC

Creating a Simple Drives Explorer Program

Rate me:
Please Sign up or sign in to vote.
4.44/5 (10 votes)
5 May 2009CPOL8 min read 95.1K   3.6K   38  
A project using the Windows Explorer Framework and some API stuff
// LeftView.cpp : implementation of the CLeftView class
//

#include "stdafx.h"
#include "FTreeBrowser.h"

#include "FTreeBrowserDoc.h"
#include "LeftView.h"
#include "RightView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLeftView

IMPLEMENT_DYNCREATE(CLeftView, CTreeView)

BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
	//{{AFX_MSG_MAP(CLeftView)
	ON_WM_CREATE()
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLeftView construction/destruction

CLeftView::CLeftView()
{
	// TODO: add construction code here

}

CLeftView::~CLeftView()
{
}

BOOL CLeftView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLeftView drawing

void CLeftView::OnDraw(CDC* pDC)
{
	CFTreeBrowserDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}


/////////////////////////////////////////////////////////////////////////////
// CLeftView printing

BOOL CLeftView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CLeftView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CLeftView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CLeftView::OnInitialUpdate()
{
	CTreeView::OnInitialUpdate();

	// TODO: You may populate your TreeView with items by directly accessing
    //  its tree control through a call to GetTreeCtrl().
    GetTreeCtrl().ModifyStyle(NULL, TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT);


}

/////////////////////////////////////////////////////////////////////////////
// CLeftView diagnostics

#ifdef _DEBUG
void CLeftView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CLeftView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

CFTreeBrowserDoc* CLeftView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFTreeBrowserDoc)));
	return (CFTreeBrowserDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLeftView message handlers

int CLeftView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CTreeView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
    GetDocument()->pLeftView = this;
	return 0;
}

void CLeftView::CreateRoots()
{
    // If there is anything in the tree, remove it
    GetTreeCtrl().DeleteAllItems();

    CTreeCtrl &ctlDrives = this->GetTreeCtrl();
    m_TreeImages.Create(IDB_TREE_BMP, 16, 1, RGB(255, 255, 255));

    ctlDrives.SetImageList(&m_TreeImages, TVSIL_NORMAL);

    HTREEITEM hRoot;

    char * strBuffer= NULL;
    CString strMessage;

    int nPos = 0;
    UINT nCount = 0;
    CString strDrive = "?:\\";

    DWORD dwDriveList = ::GetLogicalDrives ();

    CString cTmp;

    while (dwDriveList) {
        if (dwDriveList & 1) {
            cTmp = strDrive;
            strDrive.SetAt (0, 0x41 + nPos);

            strDrive = strDrive.Left(2);
            hRoot = ctlDrives.InsertItem(strDrive,0, 1);
        }
        dwDriveList >>= 1;
        nPos++;
    }

}

void CLeftView::OpenFolder(CString CStrPath)
{
	CTreeCtrl &ctlFolders = this->GetTreeCtrl();
	HTREEITEM hRoot;
	HTREEITEM hFolder;

	hRoot = ctlFolders.GetSelectedItem();


	HTREEITEM hChild = ctlFolders.GetChildItem(hRoot);

	// Clear the selected node
	while(hChild != 0){
		ctlFolders.DeleteItem(hChild);
		hChild = ctlFolders.GetChildItem(hRoot);
		}


	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;


	CStrPath = CStrPath + "\\*.*";

	hFind = FindFirstFile(CStrPath, &FindFileData);

	if (hFind != INVALID_HANDLE_VALUE){
		do {

			long lFSize = FindFileData.nFileSizeLow;
			CString strFSize="";

			//Want folders that aren't . and ..
			if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
				&& FindFileData.cFileName != CString (".")
				&& FindFileData.cFileName != CString (".."))
					hFolder = ctlFolders.InsertItem(FindFileData.cFileName,2,3,hRoot);

		}while((::WaitForSingleObject(m_hStopEvent, 0) != WAIT_OBJECT_0) && (::FindNextFile(hFind, &FindFileData)));
				::FindClose(hFind);;
	}
}

void CLeftView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;

    CFTreeBrowserDoc *pDoc = GetDocument();

    // Get a reference to the tree control
    CTreeCtrl &ctlFiles = this->GetTreeCtrl();

    // Find out what item is selected in the tree
    HTREEITEM nodSelected = ctlFiles.GetSelectedItem();
    // Get the string of the selected node
    CString strSelected = ctlFiles.GetItemText(nodSelected);

    HTREEITEM nodParent = nodSelected;

    //Build the full path with wild cards
    do {
            nodParent = ctlFiles.GetParentItem(nodParent);
            if (nodParent!=NULL)
                strSelected = ctlFiles.GetItemText(nodParent) + "\\" + strSelected;
    } while (nodParent != NULL);
    CString strSearchPath = strSelected + "\\*.*";

    pDoc->pRightView->DisplayFiles(strSearchPath.GetBuffer(1));

    OpenFolder(strSelected);

}

void CLeftView::OnRightViewFolderSelected(CString strPath, UINT index)
{
   CFTreeBrowserDoc *pDoc = GetDocument();

    // Get a reference to the tree control
    CTreeCtrl &ctlFolders = this->GetTreeCtrl();

    // Find out what item is selected in the tree
    HTREEITEM nodSelected = ctlFolders.GetSelectedItem();

    //Open up the branch
    ctlFolders.Expand(nodSelected, TVE_EXPAND);

    int count=0;
    HTREEITEM nodChild;
    nodChild = ctlFolders.GetChildItem(nodSelected);
    if (index > 0){
        do{
            nodChild = ctlFolders.GetNextItem(nodChild,TVGN_NEXT);
            ++count;
        }while (count < (int)index);
    }

    if (nodChild != NULL)
    {
        ctlFolders.SelectItem(nodChild);
        ctlFolders.Expand(nodChild, TVE_EXPAND);

        nodSelected = nodChild;

        // Get the string of the selected node
        CString strSelected = ctlFolders.GetItemText(nodSelected);

        HTREEITEM nodParent = nodSelected;

        //Build the full path with wild cards
        do {
                nodParent = ctlFolders.GetParentItem(nodParent);
                if (nodParent!=NULL)
                    strSelected = ctlFolders.GetItemText(nodParent) +
                          "\\" + strSelected;
        } while (nodParent != NULL);
        CString strSearchPath = strSelected + "\\*.*";
        pDoc->pRightView->DisplayFiles(strSearchPath.GetBuffer(1));
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom

Comments and Discussions