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

📄 mainfrm.cpp

📁 学习网络编程的初学者必看的.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	}
	szFtpInfo="文件接收结束,退回";
	szFtpInfo+=OldLoc;
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	::Sleep(20);
	if(IsFree)
	{
		::SendMessage(pLocView->m_hWnd,WM_REDISPLAYFILE,(WPARAM)(LPCTSTR)OldLoc,0);
	}

	delete[] pInfo;
	delete pInf;
	InternetCloseHandle(hFtpConn);
	InternetCloseHandle(hInetSession);
	return 0L;

}

UINT CMyAppThread::DeleteFileThread(LPVOID lParam)
{
	IsFree=TRUE;
	CString szFtpInfo;
	CFtpParaClass::FILE_COUNT_INFO* pInfo=(CFtpParaClass::FILE_COUNT_INFO*)lParam;
	BOOL Result;
	CString strFtp,OldFtp;
	HINTERNET hInetSession=InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	szFtpInfo="正在连接";
	szFtpInfo+=szFtpName;
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,
		szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
	if(!hFtpConn)
	{
		szFtpInfo="未连接上";
		szFtpInfo+=szFtpName;
		szFtpInfo+="服务器";
		pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
		::Sleep(10);
		InternetCloseHandle(hInetSession);
		return 0L;
	}
	szFtpInfo="成功连上";
	szFtpInfo+=szFtpName;
	szFtpInfo+="服务器,现在进入";
	szFtpInfo+=szFtpDirectory;
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	FtpSetCurrentDirectory(hFtpConn,szFtpDirectory);
    OldFtp=szFtpDirectory;
	for(int i=0;i<nCount;i++)
	{
		strFtp=OldFtp;
		CString DirName=pInfo[i].fileName;
		if(pInfo[i].ufileFlag==FILEICON)
		{
			szFtpInfo="正在删除";
			szFtpInfo+=DirName;
			pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			if(FtpDeleteFile(hFtpConn,DirName))
			{
				continue;
			}
			else
			{
				szFtpInfo="删除";
				szFtpInfo+=DirName;
				szFtpInfo+="出错,返回";
				pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				break;
			}
		}
		else
		{
			if(strFtp.Right(1)=='/')
				strFtp+=DirName;
			else
				strFtp+="/"+DirName;
			if(Result=PreDeleteFile(hInetSession,strFtp,OldFtp))
				continue;
			else
			{
				AfxMessageBox("删除出错,请查明原因");
				break;
			}
		}
	}
	szFtpInfo="文件删除结束";
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	::Sleep(10);
		if(IsFree)
			::SendMessage(pServView->m_hWnd,WM_REDISPLAYFILE,(WPARAM)(LPCTSTR)OldFtp,0);
	delete[] pInfo;
	InternetCloseHandle(hFtpConn);
	InternetCloseHandle(hInetSession);
	return 0L;

}

UINT CMyAppThread::RenameFileThread(LPVOID lParam)
{
	CString szFtpInfo;
	CFtpParaClass::FILE_CHANGE_INFO* pInfo=(CFtpParaClass::FILE_CHANGE_INFO*)lParam;
	HINTERNET hInetSession=InternetOpen(szAppName,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	szFtpInfo="正在连接";
	szFtpInfo+=szFtpName;
	szFtpInfo+="服务器";
    pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,
		szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
	if(!hFtpConn)
	{
		szFtpInfo="未连上";
		szFtpInfo+=szFtpName;
		szFtpInfo+="服务器,请查明原因,目前只能返回";
		pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
		InternetCloseHandle(hInetSession);
		::Sleep(10);
		return 0L;
	}
	szFtpInfo=szFtpName;
	szFtpInfo+="服务器连接成功,现进行改名操作";
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	FtpSetCurrentDirectory(hFtpConn,szFtpDirectory);
	szFtpInfo="正在把";
	szFtpInfo+=pInfo->OldName;
	szFtpInfo+="改为";
	szFtpInfo+=pInfo->NewName;
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
    FtpRenameFile(hFtpConn,pInfo->OldName,pInfo->NewName);
	szFtpInfo="改名成功";
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	::Sleep(10);
	delete pInfo;
	InternetCloseHandle(hFtpConn);
	InternetCloseHandle(hInetSession);
	return 0L;

}

BOOL CMyAppThread::PreDeleteFile(HINTERNET hInetSession, CString& strFtp,CString& OldDir)
{
	CString szFtpInfo;
	BOOL result;
	DWORD dwSize=MAX_PATH;
	CString tFile;
	WIN32_FIND_DATA findData;
	HINTERNET hFind;
	HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
	if(!hFtpConn)
	{
		AfxMessageBox("建立联接出错,返回");
		InternetCloseHandle(hInetSession);
		return FALSE;
	}
	szFtpInfo="正在进入";
	szFtpInfo+=strFtp;
	szFtpInfo+="目录";
    pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	FtpSetCurrentDirectory(hFtpConn,strFtp);
	szFtpInfo="进入";
	szFtpInfo+=strFtp;
	szFtpInfo+="目录成功";
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	if(!(hFind=FtpFindFirstFile(hFtpConn,_T("*"),&findData,0,0)))
	{
		if (GetLastError()!= ERROR_NO_MORE_FILES)
			result=FALSE;
		else
			result=TRUE;
		goto end;
	}
	do
	{
		tFile=findData.cFileName;
		if(tFile=="."||tFile=="..")continue;
		if(findData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
		{
			if(!(result=PreDeleteFile(hInetSession,strFtp+"/"+tFile,strFtp)))
			{
				AfxMessageBox("删除出错,返回");
				break;
			}
		}
		else
		{
			szFtpInfo="正在删除";
			szFtpInfo+=tFile;
			pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
			result=(FtpDeleteFile(hFtpConn,tFile))?TRUE:FALSE;
			if(!result)
			{
				szFtpInfo="删除"+tFile+"出错";
				pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
				break;
			}
		}
	}while(InternetFindNextFile(hFind,&findData));
	FtpSetCurrentDirectory(hFtpConn,OldDir);
	szFtpInfo="正在删除"+strFtp+"目录";
	pFtpInfoView->PostMessage(WM_RECORDFTPINFO,0,(LPARAM)(LPCTSTR)szFtpInfo);
	FtpRemoveDirectory(hFtpConn,strFtp);
	InternetCloseHandle(hFind);
	InternetCloseHandle(hFtpConn);
	return result;
	
end:
    InternetCloseHandle(hFtpConn);
	return result;

}
BOOL CMyAppThread::PreReceiveFile(HINTERNET hInetSession, CString& str,CString& strFtp,LPVOID pInf)
{
	CFtpParaClass:: FILE_FTP_INFO* pInfo=(CFtpParaClass:: FILE_FTP_INFO*)pInf;
	BOOL result;
	UINT uSize;
	DWORD dwSize=MAX_PATH;
	HANDLE hHeap;
	LPSTR lpBuffer;
	CString lpBufferFtp,tFile;
	WIN32_FIND_DATA findData;
	HINTERNET hFind;
	HINTERNET hFtpConn=InternetConnect(hInetSession,szFtpName,nPort,szFtpUser,szFtpPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
	if(!hFtpConn)
	{
		AfxMessageBox("建立联接出错,返回");
		InternetCloseHandle(hInetSession);
		return FALSE;
	}
	uSize=(GetCurrentDirectory(0,NULL)*sizeof(TCHAR));
	hHeap=GetProcessHeap();
	lpBuffer=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,uSize);
	GetCurrentDirectory(uSize,lpBuffer);
	
	FtpSetCurrentDirectory(hFtpConn,strFtp);
	::CreateDirectory(str,NULL);
	::SetCurrentDirectory(str);		
	::SendMessage(pLocView->m_hWnd,WM_SETDIR,0,(LPARAM)(LPCTSTR)str);
	if(!(hFind=FtpFindFirstFile(hFtpConn,_T("*"),&findData,0,0)))
	{
		if (GetLastError()!= ERROR_NO_MORE_FILES)
			result=FALSE;
		else
			result=TRUE;
		goto end;
	}
	do
	{
		tFile=findData.cFileName;
		if(tFile=="."||tFile==".."||findData.dwFileAttributes==FILE_ATTRIBUTE_HIDDEN)continue;
		if(findData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
		{
			if(!(result=PreReceiveFile(hInetSession,str+"\\"+tFile,strFtp+"/"+tFile,pInf)))
			{
				AfxMessageBox("文件发送出错,返回");
				break;
			}
		}
		else
		{
			result=(FtpGetFile(hFtpConn,tFile,tFile,FALSE,FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_BINARY|
				INTERNET_FLAG_NO_CACHE_WRITE,0))?TRUE:FALSE;
			if(!result)
			{
				AfxMessageBox("文件发送出错,返回");
				break;
			}
			if(IsFree)
			{
				IsFree=FALSE;
				strcpy(pInfo->szFileName,tFile);
				strcpy(pInfo->szFileDate,"");
				strcpy(pInfo->szFileSize,"");
				pInfo->nType=FILEICON;
				::PostMessage(pLocView->m_hWnd,WM_SETFILE,(WPARAM)pInfo,SERVFILE);
			}
		}
	}while(InternetFindNextFile(hFind,&findData));
	SetCurrentDirectory(lpBuffer);
	::SendMessage(pLocView->m_hWnd,WM_SETDIR,0,(LPARAM)lpBuffer);
	InternetCloseHandle(hFind);
	HeapFree(hHeap,HEAP_NO_SERIALIZE,lpBuffer);
	InternetCloseHandle(hFtpConn);
	return result;
	
end:
	::SetCurrentDirectory(lpBuffer);
	::SendMessage(pLocView->m_hWnd,WM_SETDIR,0,(LPARAM)lpBuffer);
	HeapFree(hHeap,HEAP_NO_SERIALIZE,lpBuffer);
	InternetCloseHandle(hFtpConn);
	return result;
}



void CLocFileView::OnDblclkLocFile(NMHDR* pNMHDR, LRESULT* pResult) 
{
    NMLVDISPINFO* pLocInfo=(NMLVDISPINFO*)pNMHDR;
	CString str,buf;
	int iIcon,iIconSel;
	str.Format("%d",pLocInfo->item);
	if(str=="-1")
		return;
	int nItem=atoi(str);
	DWORD dwFlag=m_ctLocFile.GetItemData(nItem);
	if(dwFlag==FILEICON)
	{
		OnLocFileOpen();
		return;
	}
	::GetCurrentDirectory(MAX_PATH,szLocDirectory);
	str=szLocDirectory;
	m_ctLocFile.GetItemText(nItem,0,szLocDirectory,MAX_PATH);
	buf=szLocDirectory;
	if(str.Right(1)!="\\")
		str+="\\";
	str+=szLocDirectory;
	GetFileIcon(buf,&iIcon,&iIconSel);
	int i=InsertLocDir(str,iIcon,iIconSel);
	m_ctLocDir.SetCurSel(i);
	::SetCurrentDirectory(str);
	::GetCurrentDirectory(MAX_PATH,szLocDirectory);
	BeginFindLocFile();	
	*pResult = 0;
}

void CLocFileView::OnSize(UINT nType, int cx, int cy) 
{
//	CFormView::OnSize(nType, cx, cy);
	if(m_ctLocDir.m_hWnd)
		m_ctLocDir.SetWindowPos(this,0,0,cx,0,SWP_NOZORDER|SWP_NOMOVE);
	if(m_ctLocFile.m_hWnd)
	{
		m_ctLocFile.ShowWindow(SW_HIDE);
		m_ctLocFile.SetWindowPos(this,0,0,cx,cy-25,SWP_NOZORDER|SWP_NOMOVE);
		m_ctLocFile.ShowWindow(SW_SHOW);
	}
	
}

void CServFileView::OnSize(UINT nType, int cx, int cy) 
{
//	CFormView::OnSize(nType, cx, cy);
    if(m_ctServDir.m_hWnd&&m_ctBack.m_hWnd)
	{
		m_ctServDir.SetWindowPos(this,0,0,cx-30,0,SWP_NOZORDER|SWP_NOMOVE);
		m_ctBack.SetWindowPos(this,cx-28,0,0,0,SWP_NOZORDER|SWP_NOSIZE);
	}
	if(m_ctServFile.m_hWnd)
	{
		m_ctServFile.ShowWindow(SW_HIDE);
		m_ctServFile.SetWindowPos(this,0,0,cx,cy-25,SWP_NOZORDER|SWP_NOMOVE);
		m_ctServFile.ShowWindow(SW_SHOW);
	}	
}

BOOL CServFileView::SetServImageList(UINT nBitmapID)
{
	if(m_ctServImageList.Create(nBitmapID,16,1,RGB(0,255,0)))
		m_ctServFile.SetImageList(&m_ctServImageList,LVSIL_SMALL);
	else
		return FALSE;
	
	return TRUE;
}

void CLocFileView::OnSelchangeLocDir() 
{
	CString str;
    char lpBuffer[MAX_PATH];
	::GetCurrentDirectory(MAX_PATH,lpBuffer);
	int i=m_ctLocDir.GetCurSel();
	m_ctLocDir.GetLBText(i,(LPTSTR)(LPCTSTR)str);
	i=0;
	while(1)
	{
		if(!SetCurrentDirectory(str))
		{
			i++;
			if(i<2)
			{
				AfxMessageBox("请插入磁盘再试");
				continue;
			}
			str=lpBuffer;
			if((i=m_ctLocDir.FindStringExact(-1,str))!=-1)
				m_ctLocDir.SetCurSel(i);
			else
				m_ctLocDir.SetWindowText(str);
		}
		else
		{
			::SetCurrentDirectory(str);
			GetLocCurrentDirectory();
			BeginFindLocFile();
		}
		break;
		
	}	
}

void CLocFileView::OnDropdownLocDir() 
{
	int i=m_ctLocDir.GetCount();
	for(;i>m_iDirCount-1;--i)
		m_ctLocDir.DeleteItem(i);	
}

void CServFileView::OnDblclkServFile(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NMLVDISPINFO* pLocInfo=(NMLVDISPINFO*)pNMHDR;
	CString str,buf;
	str.Format("%d",pLocInfo->item);
	if(str=="-1")
		return;
	int nItem=atoi(str);
	DWORD dwFlag=m_ctServFile.GetItemData(nItem);
	if(dwFlag==FILEICON)
		return;
	m_ctServDir.GetWindowText(str);
	if(str!="/")
		str+="/";
	str+=m_ctServFile.GetItemText(nItem,0);
	m_ctServDir.SetWindowText(str);
	m_ctServDir.Invalidate();
	::PostMessage(AfxGetApp()->m_pMainWnd->m_hWnd,WM_COMMAND,IDC_QUICKCONNECT,0);
	*pResult = 0;
}

void CLocFileView::OnBegindragLocFile(NMHDR* pNMHDR, LRESULT* pResult) 
{
	if(m_bDragging)return;
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;    	
	*pResult = 0;
	int nItem=pNMListView->iItem;
	POINT pt=pNMListView->ptAction;
	m_pDragImageList=m_ctLocFile.CreateDragImage(nItem,&pt);
	m_bDragging=TRUE;
	m_pDragImageList->BeginDrag(0,CPoint(0,0));
	m_pDragImageList->DragEnter(NULL,pt);
	SetCapture();
	SetCursor(m_hDragCursor);
	
	
	*pResult = 0;
}

void CLocFileView::OnMouseMove(UINT nFlags, CPoint point) 
{
    if(m_bDragging)
	{
		CRect rect;
		pServView->m_ctServFile.GetWindowRect(&rect);
		ClientToScreen(&point);
		if(rect.PtInRect(point))
			SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
		else
			SetCursor(m_hDragCursor);
		m_pDragImageList->DragMove(point);
	}	
	CFormView::OnMouseMove(nFlags, point);
}

void CLocFileView::OnLButtonUp(UINT nFlags, CPoint point) 

⌨️ 快捷键说明

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