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

📄 mainfrm.cpp

📁 这是主要完成文件上传功能,对要写ftp程序者是一个好的参考源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//		m_wndDlgBar.SetDlgItemText(IDC_LOCALPATH,sProjectAttrib[iNowProjectNo][5]);
		pProjectTree->PostMessage(WM_MY_PROJECT_ADD);
	}
}

void CMainFrame::OnProjectDelete() 
{
	// TODO: Add your command handler code here
	if(MessageBox("确实要将当前所选的项目删除?","确定删除项目",MB_YESNO)==IDYES)
	{
		pProjectTree->PostMessage(WM_MY_PROJECT_DELETE);
	}
}

void CMainFrame::OnMyExit(WPARAM wParam, LPARAM lParam)
{
	DestroyWindow();
}

void CMainFrame::OnWebLogoLoop(WPARAM wParam, LPARAM lParam)
{
	CString csMessage;
	csMessage.Format("服务器第%d次重连(共%d次)\r\n",iWebTryLoop,iWebLoop);
	if((iWebTryLoop++)<iWebLoop)
	{
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
		if((MyFtpLogoThread = AfxBeginThread(MyFtpLogoThreadFunction,0)) == NULL)
		{
			//建立失败处理
			return;
		}
		else    //函数会建立一个辅助线程并暂时将其挂起。
		{
			MyFtpLogoThread->SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
			MyFtpLogoThread->ResumeThread();//调用CWinThread:: ResumeThread使线程开始运行
		}
	}
	else//错误过多
	{
		bAutoLoad=false;
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"服务器重连失败\r\n");
		if(bAutoHangUp)//需要自动挂断
		{
			HangUp();
		}
		if(bShutDownWhenFinish)//完成后关闭计算机
		{
			PostMessage(WM_MY_SHUTDOWN);
		}
		///错误的时候不退出程序
		//if(bQuitWhenFinish)	//完成后退出
		//{
		//	pMainFrame->PostMessage(WM_MY_APP_EXIT);
		//}
	}
}


void CMainFrame::OnWebUpLoop(WPARAM wParam, LPARAM lParam)
{
	CString csMessage;
	csMessage.Format("服务器第%d次重连(共%d次)\r\n",iWebTryLoop,iWebLoop);
	if((iWebTryLoop++)<iWebLoop)
	{
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
		
		if((MyFtpUpThread = AfxBeginThread(MyFtpUpThreadFunction,0)) == NULL)
		{
			//建立失败处理
			return;
		}
		else    //函数会建立一个辅助线程并暂时将其挂起。
		{
			MyFtpUpThread->SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
			MyFtpUpThread->ResumeThread();//调用CWinThread:: ResumeThread使线程开始运行
		}
	}
	else//错误过多
	{
		bAutoLoad=false;
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"服务器重连失败\r\n");
		if(bAutoHangUp)//需要自动挂断
		{
			HangUp();
		}
		if(bShutDownWhenFinish)//完成后关闭计算机
		{
			PostMessage(WM_MY_SHUTDOWN);
		}
		///错误的时候不退出程序
		//if(bQuitWhenFinish)	//完成后退出
		//{
		//	pMainFrame->PostMessage(WM_MY_APP_EXIT);
		//}
	}
}


void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	switch(nIDEvent)
	{
	case 101:
		///更改任务图标****************
		if((!bLinkDataGood))
		{
			if(iIconNumber!=99)
			{
				iIconNumber=99;
				NOTIFYICONDATA tnid; 
				tnid.cbSize = sizeof(NOTIFYICONDATA); 
				tnid.hWnd = GetSafeHwnd();
				tnid.uID = WM_SCNOTIFYICON; 
				tnid.uFlags = NIF_TIP|NIF_ICON; 
				memset(tnid.szTip, 0, 32);
				lstrcpy(tnid.szTip, "EasyFtp㊣");//显示浮动提示
				tnid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
				Shell_NotifyIcon(NIM_MODIFY, &tnid); 
			}
		}
		else
		{
			NOTIFYICONDATA tnid; 
			tnid.cbSize = sizeof(NOTIFYICONDATA); 
			tnid.hWnd = GetSafeHwnd();
			tnid.uID = WM_SCNOTIFYICON; 
			tnid.uFlags = NIF_TIP|NIF_ICON; 
			memset(tnid.szTip, 0, 32);
			lstrcpy(tnid.szTip, "EasyFtp㊣");//显示浮动提示
			switch(iIconNumber)
			{
			case 0:
				tnid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON2);
				iIconNumber++;
				break;
			case 1:
				tnid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON3);
				iIconNumber++;
				break;
			case 2:
				tnid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON4);
				iIconNumber=0;
				break;
			default:
				tnid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON2);
				iIconNumber=0;
				break;
			}
			Shell_NotifyIcon(NIM_MODIFY, &tnid); 
		}
		////***********完成
		break;
	default:
		break;
	}
	CFrameWnd::OnTimer(nIDEvent);
}


UINT MyMUpThreadFunction(LPVOID lpParam)
{
	HINTERNET hInetSession;
	HINTERNET hFtpConn;
	CString csMessage;
	unsigned int j;
	int i;
	int iNowNo=(int)lpParam/10;	//取得任务号码
	int iThreadNo=(int)lpParam%10;//取得线程号码
	int iErrorLoopTime=0;		//每个工作失败允许次数
	char temp1[256],temp2[256],temp[256],*ptemp;
	csMessage.Format("线程%d开始%s文件的上传任务\r\n",iThreadNo,pWorkView->m_WorkList.GetItemText(iNowNo,0));
	pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
	pWorkView->m_WorkList.SetItemText(iNowNo,5,"上传");
MThreadLink:
	csMessage.Format("EasyFtp-UpThread%02d-%02d",iThreadNo,iErrorLoopTime);
	hInetSession=InternetOpen(csMessage,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	csMessage="";
	hFtpConn=InternetConnect(hInetSession,csName,21,
		csUser,csPassword,INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);
	if(!hFtpConn)
	{
		while(!(InternetCloseHandle(hInetSession)))
		{
			csMessage.Format("线程%d连接没有关闭,等待...\r\n",iThreadNo);
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
			if(bExitApp)
				return 0;
		}
		if(iErrorLoopTime<iWebLoop)
		{
			iErrorLoopTime++;
			csMessage.Format("线程%d连接失败,现在第%d次(共%d次)重试\r\n",iThreadNo,iErrorLoopTime,iWebLoop);
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
			goto MThreadLink;
		}
		//csMessage.Format("线程%d连接放弃,重试下一个工作\r\n",iThreadNo);
		csMessage.Format("线程%d连接放弃,可能由于服务器忙,本线程暂停\r\n",iThreadNo);
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
		pWorkView->m_WorkList.SetItemText(iNowNo,5,"放弃");
		//if(iMThreadWorkNowNo<iMThreadWorkTotal)//还有任务没有分配
		//{
		//	iNowNo=iMThreadWorkNowNo++;
		//	pWorkView->m_WorkList.SetItemText(iNowNo,5,"上传");
		//	iErrorLoopTime=0;		//计数器归零
		//	goto MThreadLink;	//新建任务连接
		//}
		//else//没有任务可以分配
		//{
			bMThreadWorkReady[iThreadNo]=true;	//线程空闲
			goto MThreadOK;
		//}
	}
	bLinkDataGood=true;
MThreadSendNext:
	strcpy(temp1,csLocalPath);
	strcat(temp1,"\\");
	strcat(temp1,pWorkView->m_WorkList.GetItemText(iNowNo,3));
	strcat(temp1,pWorkView->m_WorkList.GetItemText(iNowNo,0));
	strcpy(temp2,"/");
	strcat(temp2,csWebPath);
	if(strcmp(temp2,"/")==0)
	{
	}
	else
	{
		strcat(temp2,"/");
	}
	strcat(temp2,pWorkView->m_WorkList.GetItemText(iNowNo,3));
	strcat(temp2,pWorkView->m_WorkList.GetItemText(iNowNo,0));
	for(j=0;j<(strlen(temp2)+1);j++)
	{
		if(temp2[j]=='\\')
		{
			temp2[j]='/';
		}
	}
	strcpy(temp,strlwr(temp1));
	strcpy(temp1,temp);
	//强制小写
	strcpy(temp,strlwr(temp2));
	strcpy(temp2,temp);
	//强制小写
mtputfileagain:
	bLinkDataGood=true;
	if(FtpPutFile(hFtpConn,temp1,temp2,INTERNET_FLAG_TRANSFER_BINARY ,0))
	{
		csMessage.Format("线程%d发送",iThreadNo);
		csMessage+=temp1;
		csMessage+="->";
		csMessage+=temp2;
		csMessage+="成功\r\n";
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
		pWorkView->m_WorkList.SetItemText(iNowNo,5,"完成");
		//完成任务后回填********************
		pWebView->PostMessage(WM_MY_WEB_TOLOCAL,0,(LPARAM)iNowNo);
		//****************************
		if(iMThreadWorkNowNo<iMThreadWorkTotal)
			//还有任务没有分配
		{
			iNowNo=iMThreadWorkNowNo++;
			csMessage.Format("线程%d开始执行新的%s文件上传任务\r\n",iThreadNo,pWorkView->m_WorkList.GetItemText(iNowNo,0));
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
			pWorkView->m_WorkList.SetItemText(iNowNo,5,"上传");
			goto MThreadSendNext;
		}
		else
		{
			bMThreadWorkReady[iThreadNo]=true;	//线程空闲
			goto mtend;
		}
	}
	else
	{
		if(GetLastError()==12003)//路径非法
		{
			strcpy(temp,temp2);
			ptemp=strrchr(temp,'/');
			if(ptemp)
			{
				temp[strlen(temp)-strlen(ptemp)]=0x00;
				if(FtpCreateDirectory(hFtpConn,temp))
					goto mtputfileagain;
				//建立目录
			}
			
		}
		else//不是目录问题
		{		//失败重试
			if((iErrorLoopTime++)<iWebTryLoop)
			{
				csMessage.Format("线程%d发送",iThreadNo);
				csMessage+=temp1;
				csMessage+="->";
				csMessage+=temp2;
				csMessage+="失败,现在重试\r\n";
				pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
				goto mtputfileagain;
			}
			csMessage.Format("线程%d发送",iThreadNo);
			csMessage+=temp1;
			csMessage+="->";
			csMessage+=temp2;
			csMessage+="失败\r\n";
			pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
			pWorkView->m_WorkList.SetItemText(iNowNo,5,"失败");
			if(iMThreadWorkNowNo<iMThreadWorkTotal)
				//还有任务没有分配
			{
				iErrorLoopTime=0;
				iNowNo=iMThreadWorkNowNo++;
				goto MThreadSendNext;
			}
			else
			{
				bMThreadWorkReady[iThreadNo]=true;	//线程空闲
				goto mtend;
			}
		}
	}
mtend:	
	while(!(InternetCloseHandle(hFtpConn)))	//关闭连接
	{
		csMessage.Format("线程%d连接没有关闭,等待...\r\n",iThreadNo);
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
		if(bExitApp)
			return 0;
	}
	while(!(InternetCloseHandle(hInetSession)))	//关闭连接
	{
		csMessage.Format("线程%d连接没有关闭,等待...\r\n",iThreadNo);
		pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)csMessage);
		if(bExitApp)
			return 0;
	}
MThreadOK:
	if(bMThreadWorkReady[0]&&
		bMThreadWorkReady[1]&&
		bMThreadWorkReady[2]&&
		bMThreadWorkReady[3]&&
		bMThreadWorkReady[4])//全部空闲
	{
		//::Sleep(iDialDelay*1000);//等待回填工作
		bLinkDataGood=false;
		bool bFinish=true;
		int iTotal=pWorkView->m_WorkList.GetItemCount();
		for(i=0;i<iTotal;i++)
		{
			strcpy(temp,pWorkView->m_WorkList.GetItemText(i,5));
			if(strcmp("完成",temp)!=0)
			{
				bFinish=false;
			}
			else//将完成的去掉
			{
				pWorkView->m_WorkList.DeleteItem(i);
				iTotal-=1;
				i-=1;
			}
		}
		if((!bFinish)&&bWebAgain)//需要重试
		{
			pMainFrame->PostMessage(WM_MY_WEBLOOP_UP);
		}
		else
		{
			//完成任务后回填********************
			pWebView->PostMessage(WM_MY_WEB_TOLOCAL,0,(LPARAM)-1);
			//****************************
			if(bAutoHangUp)//需要自动挂断
			{
				HangUp();
			}
			::Sleep(iDialDelay*1000);//等待回填工作
			if(bFinish)
			{
				pMessageView->PostMessage(WM_MY_MESSAGE_UPDATE,0,(LPARAM)(LPCTSTR)"上传完成\r\n");
				if(bShutDownWhenFinish)//完成后关闭计算机
				{
					pMainFrame->PostMessage(WM_MY_SHUTDOWN);
				}
				if(bQuitWhenFinish)	//完成后退出
				{
					pMainFrame->PostMessage(WM_MY_APP_EXIT);
				}
			}
		}
	}
	return 1;
}

void CMainFrame::OnQuickWebtolocal() 
{
	// TODO: Add your command handler code here
	bWebToLocal=!bWebToLocal;
}

void CMainFrame::OnUpdateQuickWebtolocal(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(bWebToLocal)
	{
		pCmdUI->SetCheck(1);
	}
	else
	{
		pCmdUI->SetCheck(0);
	}
}

void CMainFrame::OnQuickShutdown() 
{
	// TODO: Add your command handler code here
	bShutDownWhenFinish=!bShutDownWhenFinish;
}

void CMainFrame::OnUpdateQuickShutdown(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(bShutDownWhenFinish)
	{
		pCmdUI->SetCheck(1);
	}
	else
	{
		pCmdUI->SetCheck(0);
	}
}

void CMainFrame::OnQuickQuitwhenfinish() 
{
	// TODO: Add your command handler code here
	bQuitWhenFinish=!bQuitWhenFinish;
	
}

void CMainFrame::OnUpdateQuickQuitwhenfinish(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(bQuitWhenFinish)
	{
		pCmdUI->SetCheck(1);
	}
	else
	{
		pCmdUI->SetCheck(0);
	}
}

void CMainFrame::OnQuickAutodial() 
{
	// TODO: Add your command handler code here
	bAutoDial=!bAutoDial;		
}

void CMainFrame::OnUpdateQuickAutodial(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(bAutoDial)
	{
		pCmdUI->SetCheck(1);
	}
	else
	{
		pCmdUI->SetCheck(0);
	}
}

void CMainFrame::OnQuickHangup() 
{
	// TODO: Add your command handler code here
	bAutoHangUp=!bAutoHangUp;
}

void CMainFrame::OnUpdateQuickHangup(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(bAutoHangUp)
	{
		pCmdUI->SetCheck(1);
	}
	else
	{
		pCmdUI->SetCheck(0);
	}
}

void CMainFrame::OnQuickTimecmp() 
{
	// TODO: Add your command handler code here
	bCmpTime=!bCmpTime;
}

void CMainFrame::OnUpdateQuickTimecmp(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(bCmpTime)
	{
		pCmdUI->SetCheck(1);
	}
	else
	{
		pCmdUI->SetCheck(0);
	}	
}


void CMainFrame::OnMyShutDown(WPARAM wParam, LPARAM lParam)
{
	ExitWindowsEx(EWX_SHUTDOWN,0);
}

void CMainFrame::OnUpdateWebpath() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFrameWnd::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.

	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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