⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftplistview.cpp

📁 FTP客户端
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************/
/*																*/
/*  FtpListView.cpp												*/
/*																*/
/*  Implementation of the CFtpListView class.					*/
/*																*/
/*  Programmed by Pablo van der Meer							*/
/*  Copyright Pablo Software Solutions 2002						*/
/*	http://www.pablovandermeer.nl								*/
/*																*/
/*  Last updated: 24 june 2002									*/
/*																*/
/****************************************************************/


#include "stdafx.h"
#include "FtpWanderer.h"
#include "MainFrm.h"
#include "FtpWandererDoc.h"
#include "FtpListView.h"

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

BOOL g_bSortAscending;

IMPLEMENT_DYNCREATE(CFtpListView, CListView)

BEGIN_MESSAGE_MAP(CFtpListView, CListView)
	//{{AFX_MSG_MAP(CFtpListView)
	ON_WM_CREATE()
	ON_NOTIFY_REFLECT_EX(LVN_ENDLABELEDIT, OnEndlabeledit)
	ON_NOTIFY_REFLECT(LVN_GETDISPINFO, OnGetdispinfo)
	ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnClick)
	ON_NOTIFY_REFLECT(LVN_DELETEITEM, OnDeleteitem)
	ON_NOTIFY_REFLECT(LVN_BEGINLABELEDIT, OnBeginlabeledit)
	ON_WM_SIZE()
	ON_WM_CTLCOLOR()
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	ON_WM_DROPFILES()
	ON_NOTIFY_REFLECT(LVN_ODCACHEHINT, OnOdcachehint)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFtpListView construction/destruction

CFtpListView::CFtpListView()
{
	m_nSortedCol = 0; 
    g_bSortAscending = TRUE; 
	m_bEditMode = FALSE;
	m_bSearching = FALSE;
}

CFtpListView::~CFtpListView()
{
}


/********************************************************************/
/*																	*/
/* Function name : OnDraw											*/
/* Description   : Called by the framework to render an image of	*/
/*				   the document										*/
/*																	*/
/********************************************************************/
void CFtpListView::OnDraw(CDC* pDC)
{
	CFtpWandererDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
}


/********************************************************************/
/*																	*/
/* Function name : OnInitialUpdate									*/
/* Description   : Called by the framework after the view is first	*/
/*				   attached to the document, but before the view is */
/*				   initially displayed.								*/
/*																	*/
/********************************************************************/
void CFtpListView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

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


/********************************************************************/
/*																	*/
/* Function name : GetDocument										*/
/* Description   : Get a pointer to the view抯 document.			*/
/*																	*/
/********************************************************************/
#ifdef _DEBUG
CFtpWandererDoc* CFtpListView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFtpWandererDoc)));
	return (CFtpWandererDoc*)m_pDocument;
}
#endif //_DEBUG


/********************************************************************/
/*																	*/
/* Function name : OnStyleChanged									*/
/* Description   : View style has been changed.						*/
/*																	*/
/********************************************************************/
void CFtpListView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
{
	//TODO: add code to react to the user changing the view style of your window
}


/********************************************************************/
/*																	*/
/* Function name : OnCreate											*/
/* Description   : Called when window needs to be created.			*/
/*																	*/
/********************************************************************/
int CFtpListView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
    GetListCtrl().ModifyStyle(0, LVS_EDITLABELS);

    // column initialization
    GetListCtrl().InsertColumn(0, "Name", LVCFMT_LEFT, 180);
    GetListCtrl().InsertColumn(1, "Size", LVCFMT_RIGHT, 100);
    GetListCtrl().InsertColumn(2, "Type", LVCFMT_LEFT, 100);
	GetListCtrl().InsertColumn(3, "Created", LVCFMT_LEFT, 100);
//	GetListCtrl().InsertColumn(4, "Attributes", LVCFMT_LEFT, 100);

	// do not take ownership of the imagelist!
	GetListCtrl().ModifyStyle(0, LVS_SHAREIMAGELISTS);
	InitListViewImageLists();
	return 0;
}


/********************************************************************/
/*																	*/
/* Function name : OnBeginlabeledit									*/
/* Description   : 													*/
/*																	*/
/********************************************************************/
void CFtpListView::OnBeginlabeledit(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
	m_bEditMode = TRUE;
	*pResult = 0;
}


/********************************************************************/
/*																	*/
/* Function name : OnEndlabeledit									*/
/* Description   : 													*/
/*																	*/
/********************************************************************/
BOOL CFtpListView::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult) 
{
	m_bEditMode = FALSE;
	// route notification to CMainFrame !
	return FALSE;
}


/********************************************************************/
/*																	*/
/* Function name : AddItem											*/
/* Description   : 													*/
/*																	*/
/********************************************************************/
int CFtpListView::AddItem(int nIndex, CFtpFileFind *pFileFind)
{
	// Allocate a new ITEMINFO structure and initialize it with information about the item.
    ITEMINFO* pItem;
    try 
	{
        pItem = new ITEMINFO;
    }
    catch(CMemoryException* e) 
	{
        e->Delete();
        return -1;
    }

    pItem->strFileName = pFileFind->GetFileName();
    pItem->nFileSize = pFileFind->GetLength();
    pFileFind->GetLastWriteTime(&pItem->ftLastWriteTime);
	pItem->bIsDirectory = pFileFind->IsDirectory();
	pItem->strType = pItem->bIsDirectory ? "Folder" : GetTypeName(pItem->strFileName);
	int nImage = GetIconIndex(pItem->strFileName, pItem->bIsDirectory);
	
	// Add the item to the list view.
    LV_ITEM lvi;
    lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM; 
    lvi.iItem = nIndex; 
    lvi.iSubItem = 0; 
    lvi.iImage = nImage;
    lvi.pszText = LPSTR_TEXTCALLBACK; 
    lvi.lParam = (LPARAM) pItem;

    return GetListCtrl().InsertItem(&lvi);
}


/********************************************************************/
/*																	*/
/* Function name : AddNewFolder										*/
/* Description   : Add new folder entry								*/
/*																	*/
/********************************************************************/
int CFtpListView::AddNewFolder(int nIndex, LPCTSTR lpszFolder)
{
	// Allocate a new ITEMINFO structure and initialize it with information about the item.
    ITEMINFO* pItem;
    try 
	{
        pItem = new ITEMINFO;
    }
    catch(CMemoryException* e) 
	{
        e->Delete();
        return -1;
    }

    pItem->strFileName = lpszFolder;
    pItem->nFileSize = 0;
	
	SYSTEMTIME systemTime;
	GetSystemTime(&systemTime);
	::SystemTimeToFileTime(&systemTime, &pItem->ftLastWriteTime);

	pItem->bIsDirectory = TRUE;
	pItem->strType = "Folder";
	int nImage = GetIconIndex(pItem->strFileName, pItem->bIsDirectory);
	
	// Add the item to the list view.
    LV_ITEM lvi;
    lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM; 
    lvi.iItem = nIndex; 
    lvi.iSubItem = 0; 
    lvi.iImage = nImage;
    lvi.pszText = LPSTR_TEXTCALLBACK; 
    lvi.lParam = (LPARAM) pItem;

    return GetListCtrl().InsertItem(&lvi);
}


/********************************************************************/
/*																	*/
/* Function name : AddNewFile										*/
/* Description   : Add new file entry								*/
/*																	*/
/********************************************************************/
int CFtpListView::AddNewFile(int nIndex, LPCTSTR lpszFileName, DWORD dwFileSize)
{
    int nCount = GetListCtrl().GetItemCount();
    for(int i=0; i<nCount; i++)
	{
		if (GetListCtrl().GetItemText(i, 0).CompareNoCase(lpszFileName) == 0)
		{
			// file already in the list
			return -1;
		}
	}

	// Allocate a new ITEMINFO structure and initialize it with information about the item.
    ITEMINFO* pItem;
    try 
	{
        pItem = new ITEMINFO;
    }
    catch(CMemoryException* e) 
	{
        e->Delete();
        return -1;
    }

    pItem->strFileName = lpszFileName;
    pItem->nFileSize = dwFileSize;
	
	SYSTEMTIME systemTime;
	GetSystemTime(&systemTime);
	::SystemTimeToFileTime(&systemTime, &pItem->ftLastWriteTime);

	pItem->bIsDirectory = FALSE;
	pItem->strType = GetTypeName(pItem->strFileName);;
	int nImage = GetIconIndex(pItem->strFileName, pItem->bIsDirectory);
	
	// Add the item to the list view.
    LV_ITEM lvi;
    lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM; 
    lvi.iItem = nIndex; 
    lvi.iSubItem = 0; 
    lvi.iImage = nImage;
    lvi.pszText = LPSTR_TEXTCALLBACK; 
    lvi.lParam = (LPARAM) pItem;

    return GetListCtrl().InsertItem(&lvi);
}



/********************************************************************/
/*																	*/
/* Function name : FreeItemMemory									*/
/* Description   : Free memory that was allocated for list item.	*/
/*																	*/
/********************************************************************/
void CFtpListView::FreeItemMemory()
{
    int nCount = GetListCtrl().GetItemCount();
    if(nCount) 
	{
        for(int i=0; i<nCount; i++)
            delete(ITEMINFO*) GetListCtrl().GetItemData(i);
    }
}


/********************************************************************/
/*																	*/
/* Function name : OnGetdispinfo									*/
/* Description   : Give listview the string it has to display		*/
/*																	*/
/********************************************************************/
void CFtpListView::OnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult) 
{
    CString string;
	LV_DISPINFO* pDispInfo =(LV_DISPINFO*) pNMHDR;

    if(pDispInfo->item.mask & LVIF_TEXT) 
	{
		ITEMINFO* pItem =(ITEMINFO*) pDispInfo->item.lParam;

        switch(pDispInfo->item.iSubItem) 
		{
        case 0: // File name
            ::lstrcpy(pDispInfo->item.pszText, pItem->strFileName);
            break;

        case 1: // File size
			if (pItem->bIsDirectory)
			{
				::lstrcpy(pDispInfo->item.pszText, "");
			}
			else
			{
				::lstrcpy(pDispInfo->item.pszText, FormatSize(pItem->nFileSize));
			}
            break;

        case 2: // File type
            ::lstrcpy(pDispInfo->item.pszText, pItem->strType);

			break;
        case 3: // Date and time
            CTime time(pItem->ftLastWriteTime);

            BOOL pm = FALSE;
            int nHour = time.GetHour();
            if(nHour == 0)
                nHour = 12;
            else 
			if(nHour == 12)
                pm = TRUE;
            else 
			if(nHour > 12) 
			{
                nHour -= 12;
                pm = TRUE;
            }

            string.Format(_T("%d/%0.2d/%0.2d(%d:%0.2d%c)"),
                time.GetMonth(), time.GetDay(), time.GetYear() % 100,
                nHour, time.GetMinute(), pm ? _T('p') : _T('a'));
            ::lstrcpy(pDispInfo->item.pszText, string);
            break;
        }
    }
	*pResult = 0;
}


/********************************************************************/
/*																	*/
/* Function name : OnColumnClick									*/
/* Description   : 													*/
/*																	*/
/********************************************************************/
void CFtpListView::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView =(NM_LISTVIEW*) pNMHDR;

	// User clicked on header using left mouse button
    if(pNMListView->iSubItem == m_nSortedCol)
		g_bSortAscending = !g_bSortAscending;
	else
		g_bSortAscending = TRUE;

	m_nSortedCol = pNMListView->iSubItem;
    
	GetListCtrl().SortItems(CompareFunc, pNMListView->iSubItem);
	*pResult = 0;
}


/********************************************************************/
/*																	*/
/* Function name : CompareFunc										*/
/* Description   : 													*/
/*																	*/
/********************************************************************/
int CALLBACK CFtpListView::CompareFunc(LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort)
{
    ITEMINFO* pItem1 =(ITEMINFO*) lParam1;
    ITEMINFO* pItem2 =(ITEMINFO*) lParam2;
    int nResult;

    switch(lParamSort) 
	{
    case 0: // File name
		if (pItem1->bIsDirectory && !pItem2->bIsDirectory)
			return (g_bSortAscending ? -1 : 1);

		if (!pItem1->bIsDirectory && pItem2->bIsDirectory)
			return (g_bSortAscending ? 1 : -1);

        nResult = pItem1->strFileName.CompareNoCase(pItem2->strFileName);
        break;

    case 1: // File size
		if (pItem1->bIsDirectory && !pItem2->bIsDirectory)
			return (g_bSortAscending ? -1 : 1);
		if (!pItem1->bIsDirectory && pItem2->bIsDirectory)
			return (g_bSortAscending ? 1 : -1);

        nResult = pItem1->nFileSize - pItem2->nFileSize;
        break;

    case 2: // type
		if (pItem1->bIsDirectory && !pItem2->bIsDirectory)
			return -1;
		if (!pItem1->bIsDirectory && pItem2->bIsDirectory)
			return 1;

        nResult = pItem1->strType.CompareNoCase(pItem2->strType);
        break;

    case 3: // Date and time
        nResult = ::CompareFileTime(&pItem1->ftLastWriteTime, &pItem2->ftLastWriteTime);
        break;
    }
    return (g_bSortAscending ? nResult : -nResult);
}


/********************************************************************/
/*																	*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -