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

📄 ftplistview.cpp

📁 FTP客户端
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Function name : GetTypeName										*/
/* Description   : 													*/
/*																	*/
/********************************************************************/
char* CFtpListView::GetTypeName(CString strPath)
{
	SHFILEINFO sfi;
	memset(&sfi, 0, sizeof(sfi));

	static char lpBuff[MAX_PATH];
	lpBuff[0] = char ('\0');

	SHGetFileInfo(strPath, 0, &sfi, sizeof(sfi), SHGFI_TYPENAME);

	lstrcpy(lpBuff, sfi.szTypeName);
	if (lpBuff[0] == char('\0'))
	{
		int nDotIdx = strPath.ReverseFind('.');
		int nBSIdx = strPath.ReverseFind('\\');
		if (nDotIdx > nBSIdx)
		{
			strPath = strPath.Mid(nDotIdx+1);
			strPath.MakeUpper();
			lstrcpy (lpBuff, strPath + ' ');
		}

		lstrcat (lpBuff, _T("File"));
	}
	return lpBuff;
}


/********************************************************************/
/*																	*/
/* Function name : FormatSize										*/
/* Description   : Format size, the way explorer diplays it			*/
/*																	*/
/********************************************************************/
char* CFtpListView::FormatSize(DWORD dwSizeLow, DWORD dwSizeHigh)
{
	static char szBuff[100];

	unsigned __int64 nFileSize = ((unsigned __int64)(((DWORD)(dwSizeLow)) | 
								 ((unsigned __int64)((DWORD)(dwSizeHigh))) << 32));
	unsigned __int64 kb = 1;

	if (nFileSize > 1024)
	{
		kb = nFileSize / 1024;
		if (nFileSize % 1024)
			kb++;
	}

	// make it a string
	_ui64tot(kb, szBuff, 10);

	// add thousand seperators
	int nLength = lstrlen(szBuff);
	if (nLength > 3)
	{
		LPCTSTR ptr = szBuff;
		ptr += (nLength-1);

		char szTemp[100];

		LPTSTR ptrTemp = szTemp;
		for(int i=0; i<nLength; i++)
		{
			if (i && ((i % 3) == 0)) 
			{
				if (*ptrTemp != ',')
				{
					*ptrTemp = ',';
					ptrTemp = _tcsinc(ptrTemp);
				}
			}
			*ptrTemp = *ptr;
			ptrTemp = _tcsinc(ptrTemp);
			ptr = _tcsdec(szBuff, ptr);
		}
		// terminate string
		*ptrTemp = '\0';
		// reverse string
		_tcsrev(szTemp);
		lstrcpy(szBuff, szTemp);
	}
	// add 'KB' to it
	lstrcat(szBuff, " KB");
	return szBuff;
}


/********************************************************************/
/*																	*/
/* Function name : IsDirectory										*/
/* Description   : Is nItem a directory ?							*/
/*																	*/
/********************************************************************/
BOOL CFtpListView::IsDirectory(int nItem)
{
	return ((ITEMINFO*)GetListCtrl().GetItemData(nItem))->bIsDirectory;
}


/********************************************************************/
/*																	*/
/* Function name : GetFileSize										*/
/* Description   : Get size of selected file						*/
/*																	*/
/********************************************************************/
DWORD CFtpListView::GetFileSize(int nItem)
{
	return ((ITEMINFO*)GetListCtrl().GetItemData(nItem))->nFileSize;
}


/********************************************************************/
/*																	*/
/* Function name : GetLastWriteTime									*/
/* Description   :													*/
/*																	*/
/********************************************************************/
FILETIME CFtpListView::GetLastWriteTime(int nItem)
{
	return ((ITEMINFO*)GetListCtrl().GetItemData(nItem))->ftLastWriteTime;
}


/********************************************************************/
/*																	*/
/* Function name : OnDeleteitem										*/
/* Description   : Delete allocated item data						*/
/*																	*/
/********************************************************************/
void CFtpListView::OnDeleteitem(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	delete(ITEMINFO*) GetListCtrl().GetItemData(pNMListView->iItem);
	
	*pResult = 0;
}


/********************************************************************/
/*																	*/
/* Function name : Sort												*/
/* Description   : Call sort function								*/
/*																	*/
/********************************************************************/
void CFtpListView::Sort(int nCol)
{
	GetListCtrl().SortItems(CompareFunc, (nCol != -1) ? nCol : m_nSortedCol);
}


/********************************************************************/
/*																	*/
/* Function name : InitListViewImageLists							*/
/* Description   : Use system imagelist for the file icons			*/
/*																	*/
/********************************************************************/
BOOL CFtpListView::InitListViewImageLists()
{
	HIMAGELIST himlSmall;
	HIMAGELIST himlLarge;
	SHFILEINFO sfi;

	himlSmall = (HIMAGELIST) SHGetFileInfo ((LPCSTR) "C:\\", 
		0, &sfi, sizeof (SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);

	himlLarge = (HIMAGELIST) SHGetFileInfo ((LPCSTR) "C:\\", 
		0, &sfi, sizeof (SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_LARGEICON);

	if (himlSmall && himlLarge)
	{
		::SendMessage(GetListCtrl().m_hWnd, LVM_SETIMAGELIST, (WPARAM)LVSIL_SMALL, (LPARAM)himlSmall);
		::SendMessage(GetListCtrl().m_hWnd, LVM_SETIMAGELIST, (WPARAM)LVSIL_NORMAL, (LPARAM)himlLarge);
		return TRUE;
	}
	return FALSE;
} 


/********************************************************************/
/*																	*/
/* Function name : SetFileName										*/
/* Description   : Set filename (after renaming)					*/
/*																	*/
/********************************************************************/
void CFtpListView::SetFileName(int nIndex, LPCTSTR lpszFileName)
{
	((ITEMINFO*)GetListCtrl().GetItemData(nIndex))->strFileName = lpszFileName;
	GetListCtrl().SetItemText(nIndex, 0, lpszFileName);
}


/********************************************************************/
/*																	*/
/* Function name : IsEditing										*/
/* Description   : Is listview in edit mode ?						*/
/*																	*/
/********************************************************************/
BOOL CFtpListView::IsEditing()
{
	return m_bEditMode;
}


/********************************************************************/
/*																	*/
/* Function name : PreTranslateMessage								*/
/* Description   : Prevent accelerator keys to be executed while	*/
/*				   in edit mode.									*/
/*																	*/
/********************************************************************/
BOOL CFtpListView::PreTranslateMessage(MSG* pMsg) 
{
	if(m_bEditMode)
	{
		if((pMsg->message >= WM_KEYFIRST) && (pMsg->message <= WM_KEYLAST))
		{
			TranslateMessage(pMsg);
			DispatchMessage(pMsg);
			return TRUE;
		}
	}
	return CListView::PreTranslateMessage(pMsg);
}


/********************************************************************/
/*																	*/
/* Function name : ActivateAnimation								*/
/* Description   : Switch search on/off.							*/
/*																	*/
/********************************************************************/
void CFtpListView::ActivateAnimation(BOOL bOn)
{
	if (bOn)
	{
		// control is already created
		if (IsWindow(m_AnimateCtrl.m_hWnd))
			return;

		CRect rc; 
		GetClientRect(rc);
		
		// create animation control
		if (m_AnimateCtrl.Create(WS_CHILD | ACS_CENTER | ACS_TRANSPARENT | ACS_AUTOPLAY, rc, this, 88))
		{
			// switch off redraw of listview to prevent flickering
			GetListCtrl().SetRedraw(FALSE);
			// load animation
			m_AnimateCtrl.Open(IDR_AVI3);
			// show animation
			m_AnimateCtrl.ShowWindow(SW_SHOW);
			m_bSearching = TRUE;
		}
	}
	else
	{
		m_bSearching = FALSE;
		if (IsWindow(m_AnimateCtrl.m_hWnd))
		{
			// stop animation
			m_AnimateCtrl.Stop();
			// hide animation
			m_AnimateCtrl.ShowWindow(SW_HIDE);
			// switch on redraw of listview
			GetListCtrl().SetRedraw(TRUE);
			// destroy animation control
			m_AnimateCtrl.PostMessage(WM_CLOSE);
		}
	}
}


/********************************************************************/
/*																	*/
/* Function name : OnSize											*/
/* Description   : If search animation is active resize it.			*/
/*																	*/
/********************************************************************/
void CFtpListView::OnSize(UINT nType, int cx, int cy) 
{
	CListView::OnSize(nType, cx, cy);
	
	if (IsWindow(m_AnimateCtrl.m_hWnd))
	{
		m_AnimateCtrl.MoveWindow(0, 0, cx, cy);
		Invalidate();
		UpdateWindow();
	}
}


/********************************************************************/
/*																	*/
/* Function name : OnCtlColor										*/
/* Description   : Make search animation look good.					*/
/*																	*/
/********************************************************************/
HBRUSH CFtpListView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CListView::OnCtlColor(pDC, pWnd, nCtlColor);

	UINT id = pWnd->GetDlgCtrlID();
	// change background color of animation
	if (id == 88)
	{
		pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
		return (HBRUSH)GetSysColorBrush(COLOR_WINDOW);
	}
	return hbr;
}


/********************************************************************/
/*																	*/
/* Function name : OnRclick											*/
/* Description   : Show context menu.								*/
/*																	*/
/********************************************************************/
void CFtpListView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	CMenu menu;

	POINT pt;
	GetCursorPos(&pt);

	// get selected item
	int nIndex = GetListCtrl().GetNextItem(-1, LVNI_ALL | LVNI_SELECTED); 
	if (nIndex != -1)
	{
		if (IsDirectory(nIndex))
			menu.LoadMenu(MAKEINTRESOURCE(IDR_FOLDERPOPUP));
		else
			menu.LoadMenu(MAKEINTRESOURCE(IDR_FILEPOPUP));
	}
	else
	{
		menu.LoadMenu(MAKEINTRESOURCE(IDR_LISTPOPUP));
	}
	menu.GetSubMenu(0)->TrackPopupMenu(0, pt.x, pt.y, AfxGetMainWnd(), NULL);		

	*pResult = 0;
}


/********************************************************************/
/*																	*/
/* Function name : OnDropFiles										*/
/* Description   : Files have been dropped on the listview.			*/
/*																	*/
/********************************************************************/
void CFtpListView::OnDropFiles(HDROP hDropInfo) 
{
	char szFileName[_MAX_PATH];
	
	// determine how many files are dropped
	UINT nCount = DragQueryFile(hDropInfo, -1, NULL, 0);

	for(UINT i=0; i < nCount; i++)
	{
		// get the filename
		DragQueryFile(hDropInfo, i, szFileName, _MAX_PATH);

		CString strDropName = szFileName;

		// check if it a directory
		if ((GetFileAttributes(szFileName) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
		{
			// enumerate directory structure and add everything to queue 
			CStringArray strFileNames;
			CString strFileName, strSize, strPath, strDestination;
			RecursiveFileList(szFileName, strFileNames);

			// queue all found files
			for (int i = 0; i < strFileNames.GetSize(); i++)
			{
				AfxExtractSubString(strFileName, strFileNames.GetAt(i), 0, '|');
				AfxExtractSubString(strPath,	 strFileNames.GetAt(i), 1, '|');

				// fix path
				int nPos = strDropName.ReverseFind('\\');
				if (nPos != -1)
				{
					strDropName = strDropName.Left(nPos+1);
				}
				strDestination = strPath;
				int nLength = strDropName.GetLength();
				strDestination = strDestination.Mid(nLength);
				strDestination.Replace('\\', '/');

				((CMainFrame *)AfxGetMainWnd())->UploadFile(strPath + "\\" + strFileName, strDestination + "/" + strFileName);
			}
		}
		else
		{
			int nPos = strDropName.ReverseFind('\\');
			if (nPos != -1)
			{
				((CMainFrame *)AfxGetMainWnd())->UploadFile(strDropName, strDropName.Mid(nPos+1));
			}
		}
	}

	CListView::OnDropFiles(hDropInfo);
}


/********************************************************************/
/*																	*/
/* Function name : RecursiveFileList								*/
/* Description   : Recursively walk through directory to be able to	*/
/*				   upload complete directory structure.				*/
/*																	*/
/********************************************************************/
void CFtpListView::RecursiveFileList(LPCTSTR lpszPath, CStringArray &strFileNameArray)
{
	CFileFind fileFind;
	CStringArray strDirectoryArray;

	// add directory (without filename), so we can create the directory structure later
	CString strFileName;
	strFileName.Format("|%s", lpszPath);
	strFileNameArray.Add(strFileName);
	
	BOOL bContinue = fileFind.FindFile(CString(lpszPath) + "\\*.*");
	if (!bContinue)
	{
		// the directory is empty; just close up and return.
		fileFind.Close();
		return;
	}

	// add all files in lpszPath
	while (bContinue)
	{
		bContinue = fileFind.FindNextFile();

		if (fileFind.IsDots())
			continue;

		if (fileFind.IsDirectory())
		{
			strDirectoryArray.Add(fileFind.GetFileName());
		}
		else
		{
			strFileName.Format("%s|%s", fileFind.GetFileName(), lpszPath);
			strFileNameArray.Add(strFileName);
		}
	}
	fileFind.Close();

	// get contains of directories
	for (int i = 0; i < strDirectoryArray.GetSize(); i++)
	{
		RecursiveFileList(CString(lpszPath) + "\\" + strDirectoryArray.GetAt(i), strFileNameArray);
	}
}

void CFtpListView::OnOdcachehint(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NMLVCACHEHINT* pCacheHint = (NMLVCACHEHINT*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

⌨️ 快捷键说明

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