Click here to Skip to main content
15,896,359 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.2K   3.6K   38  
A project using the Windows Explorer Framework and some API stuff
// RightView.cpp : implementation of the CRightView class
//

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

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

#include "LeftView.h"


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

/////////////////////////////////////////////////////////////////////////////
// CRightView

IMPLEMENT_DYNCREATE(CRightView, CListView)

BEGIN_MESSAGE_MAP(CRightView, CListView)
	//{{AFX_MSG_MAP(CRightView)
	ON_WM_CREATE()
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRightView construction/destruction

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

}

CRightView::~CRightView()
{
}

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

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CRightView drawing

void CRightView::OnDraw(CDC* pDC)
{
	CFTreeBrowserDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CListCtrl& refCtrl = GetListCtrl();
	refCtrl.InsertItem(0, "Item!");
	// TODO: add draw code for native data here
}

void CRightView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().

    CListCtrl &ctlFiles = this->GetListCtrl();
    ctlFiles.ModifyStyle(NULL, LVS_REPORT);
    m_ListImages.Create(IDB_TREE_BMP, 16, 1, RGB(255, 255, 255));
    ctlFiles.SetImageList(&m_ListImages, LVSIL_SMALL);

}

/////////////////////////////////////////////////////////////////////////////
// CRightView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CRightView diagnostics

#ifdef _DEBUG
void CRightView::AssertValid() const
{
	CListView::AssertValid();
}

void CRightView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CRightView message handlers
void CRightView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
{
	//TODO: add code to react to the user changing the view style of your window
}

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

void CRightView::ResetFiles()
{
    CListCtrl &ctlLView = GetListCtrl();

    ctlLView.DeleteAllItems();
    while(ctlLView.DeleteColumn(0))
        ;

    UpdateWindow();

}

void CRightView::DisplayFiles(LPTSTR Path)
{
    CListCtrl &ctlRightView = this->GetListCtrl();

    ResetFiles();
    ctlRightView.InsertColumn(0, _T("File Name"), LVCFMT_LEFT, 160);
    ctlRightView.InsertColumn(1, _T("File Size"), LVCFMT_LEFT, 40);

    //AfxMessageBox(Path);

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

    int nItem;

    hFind = FindFirstFile(Path, &FindFileData);
    int n = 0;

    if (hFind != INVALID_HANDLE_VALUE){

        do {
            if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
                && FindFileData.cFileName != CString (".")
                && FindFileData.cFileName != CString (".."))
                {
                ++n;
                nItem = ctlRightView.InsertItem(n, "File",2);


                ctlRightView.SetItemText(nItem, 0, FindFileData.cFileName);

                long lFSize = FindFileData.nFileSizeLow;
                CString strFSize="";
                strFSize.Format("%d",lFSize);

                ctlRightView.SetItemText(nItem, 1, strFSize.GetBuffer(1));
            }
        }while((::WaitForSingleObject(m_hStopEvent, 0) !=
                WAIT_OBJECT_0) && (::FindNextFile(hFind, &FindFileData)));
                ::FindClose(hFind);;

        hFind = FindFirstFile(Path, &FindFileData);
        //n = 0;
        do {

            if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
                ++n;
                nItem = ctlRightView.InsertItem(n, "File",4);


                ctlRightView.SetItemText(nItem, 0, FindFileData.cFileName);

                long lFSize = FindFileData.nFileSizeLow;
                CString strFSize="";
                strFSize.Format("%d",lFSize);

                ctlRightView.SetItemText(nItem, 1, strFSize.GetBuffer(1));
            }
        }while((::WaitForSingleObject(m_hStopEvent, 0) !=
                WAIT_OBJECT_0) && (::FindNextFile(hFind, &FindFileData)));
                ::FindClose(hFind);;
    }
	
}

void CRightView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
    *pResult = 0;

    CFTreeBrowserDoc *pDoc = GetDocument();

    CListCtrl &ctlRightView = this->GetListCtrl();

    int nItem = ctlRightView.GetSelectionMark();
    CString string= ctlRightView.GetItemText(nItem,0);

    pDoc->pLeftView->SetFocus();
    pDoc->pLeftView->OnRightViewFolderSelected(string, nItem);
}

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