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

📄 mainfrm.cpp

📁 一个简单的视频会议VC++MFC工程文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

//文件的下载操作完成之后的处理工作,由本函数来完成。
DWORD CMainFrame::finish()
{
	int thno;
	thno=clno;
	CString aaa;
	aaa=zmfile[thno].name;
	aaa=aaa+" 文件下载正在下载。。。。\n";
	AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
	aaa.ReleaseBuffer();
	now=GetTickCount();

	/*
	DWORD WaitForMultipleObjects(
	  DWORD nCount,             // number of handles in array
	  CONST HANDLE *lpHandles,  // object-handle array
	  BOOL bWaitAll,            // wait option
	  DWORD dwMilliseconds      // time-out interval
	);
	//第三个参数设为true,那么the function returns when the state of all objects in the lpHandles array is signaled.
	  */
	//详细的解释,参见Jeffrey Richter的书《Programming Application for Microsoft Windows》
	//等待多个事件对象,这样,流程执行到这里后就被阻塞了,直至所有的下载线程都完成了下载任务之后。
	HRESULT ret=WaitForMultipleObjects(BLOCK,m_thread[thno],TRUE,INFINITE);

	int i;
	i=0;
	for(int j=0;j<BLOCK;j++)
		i+=down[thno]->good[j];
	//由于此文件已经被下载完了,所以需要把对应的down数组元素置为NULL,表示“非正在下载中”。
	down[thno]=NULL;
	end=GetTickCount();

	if(i==BLOCK)
	{
		aaa=zmfile[thno].name;
		aaa=aaa+" 文件下载完毕!\n";
		AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
		aaa.ReleaseBuffer();
		CString temp;
		m_work.m_ListCtrl->SetItemText(filedono[thno],1,"|||||||||||||||||||||||||||||||||||||||||||||||||||");
		m_work.m_ListCtrl->SetItemText(filedono[thno],4,"已完成");
		temp.Format("文件下载完毕,用时  %d  秒\n",((end-now)/1000));
		temp=zmfile[thno].name+temp;
		AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)temp.GetBuffer(0),1);
		temp.ReleaseBuffer();
		for(j=0;j<BLOCK;j++)
		{
			CString bbb;
			bbb.Format("%s.down%d",zmfile[thno].name,j);
			//The DeleteFile function deletes an existing file. 
			//删除辅助文件。
			DeleteFile(bbb.GetBuffer(0));
			bbb.ReleaseBuffer();
		}

		//我觉得,成功地下载完了之后,那个XX.down文件也可以删除了。
		temp.Format("%s.down",zmfile[thno].name);
		DeleteFile(temp);//added by yjk 
	}
	return 1;
}

//建立各下载文件的线程,参数是:用户用鼠标双击的List控件中的当前item的索引值。
void CMainFrame::createthread(int threadno)
{
	DWORD dwthread;
	//建立BLOCK个线程,同时下载一个文件的不同片段。
	for(int i=0;i<BLOCK;i++)
	{
		//
		m_thread[threadno][i]=::CreateThread(NULL,0,downthread,(LPVOID)down[threadno],0,&dwthread);
		//The SetThreadPriority function sets the priority value for the specified thread. This value, together with the priority class of the thread's process, determines the thread's base priority level. 
		::SetThreadPriority(m_thread[threadno][i],THREAD_PRIORITY_HIGHEST);
	}

	//
	::CreateThread(NULL,0,notify,(LPVOID)this,0,&dwthread);
}

//当用户用鼠标双击了“可下载文件列表”List控件的时候,将间接调用此函数,执行下载文件的操作。
//参数1:值为0,没用;参数2:是用户用鼠标双击的List控件中的当前项的在List控件中的索引值。
int CMainFrame::OnClient(WPARAM wParam,LPARAM lParam) 
{
	//下载文件
	CString str;
	int jj;//用来记录“正在同时下载的文件”的个数。
	jj=0;

	//计算正在下载的文件的个数。
	for(int i = 0; i <m_work.m_ListCtrl-> GetItemCount(); i++)
	{
		str =m_work.m_ListCtrl->GetItemText(i,4 );
		if(strcmp(str, "下载中") == 0)
		{
			jj++;
		}
	}

//	if(jj>2)//也对,等于3的时候,也满足大于2的条件。
	if(jj>=3)//这样写代码,更容易理解。
	{
		AfxMessageBox("最多同时只能下载3个文件!");
		return 0;
	}

	//GetTickCount  The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function. 
	//获取当前的时间,以毫秒计数。
	now=GetTickCount();

	//此变量的作用是:保存用户用鼠标双击的List控件中的当前项的索引值。
	int* dat;
	dat=(int *)lParam;
	//现在,clno(CMainFrame类的成员变量)的值就是:用户用鼠标双击的List控件中的当前项的
	//索引值了。
	clno=*dat;

	//down数组是用来存储被下载的文件的信息用的,所以,如果某数组元素不为NULL,就说明这个文件
	//已经开始下载了。
	if(down[clno]!=NULL)
	{
		AfxMessageBox("正在被下载!");
		return 0;
	}

	CString aaa;//
	//zmfile[clno].name中存贮的是要被下载的文件的文件名。
	aaa.Format("正在初使化 %s",zmfile[clno].name);
	aaa=aaa+" 信息。。。\n";
	//作者大量地使用了这个函数来发送消息给“子孙窗口”。我以前可从没用过这个函数。
	AfxGetMainWnd()->SendMessageToDescendants(WM_AGE1,(LPARAM)aaa.GetBuffer(0),1);
	aaa.ReleaseBuffer();

	//下载List控件中的第clno个文件,并为它建一个新cdownload类。参数是:“可下载文件列表”List控件中当前列表项的索引。
	down[clno]=new cdownload(clno);
	//开始下载,并初使化。
	int type;

	//调用cdownload::startask函数。目的是:启动一个函数,用来在“文件下载进度”List控件中
	//显示文件下载的进度,速率,完成字节数等信息。仅仅是显示信息而以,跟真正的下载操作无关。
	type=down[clno]->startask(clno);

	if(type==2)
	{
		//用户选择放弃下载此文件了。
		down[clno]=NULL;//added by yjk
		return 0;
	}

	if(type==1)
	{
		AfxMessageBox("文件已下载完!");
		down[clno]=NULL;//added by yjk
		return 0;
	}

	//文件长度小于0
	if(type==-1)
	{
		AfxMessageBox("此文件长度为零或不可读!");
		return 0;
	}

	if(type==-2)
	{
		AfxMessageBox("网络联不上!");
		return 0;
	}

	//建立BLOCK个“下载文件的线程”,参数是:用户用鼠标双击的List控件中的当前item的索引值。
	createthread(clno);
	return 1;
}

//
int CMainFrame::Onright(WPARAM wParam,LPARAM lParam) 
{
 //开始进程
CString str;
int jj;
jj=0;
	for(int i = 0; i <m_work.m_ListCtrl-> GetItemCount(); i++){
		str =m_work.m_ListCtrl->GetItemText(i,4 );
		if(strcmp(str, "下载中") == 0){
			jj++;
			
		}
	}

if(jj>2)
{AfxMessageBox("最多同时只能下载3个文件!");
return 0;
}
	now=GetTickCount();
 
		CString temp1;
 
	int* dat;
dat=(int *)lParam;
CString temp;
int noth;
noth=*dat;
temp.Format("the cleek is %d",noth);
 
//	AfxMessageBox(temp);
clno=noth;

if(down[clno]==NULL){
	AfxMessageBox("no");
return 0;
}
for( i=0;i<BLOCK;i++)
::ResumeThread(m_thread[clno][i]);
 
	return 1;
	// TODO: Add your message handler code here
	
}
int CMainFrame::Onclick(WPARAM wParam,LPARAM lParam) 
{
//停进程
CString str;
int jj;
jj=0;
	for(int i = 0; i <m_work.m_ListCtrl-> GetItemCount(); i++){
		str =m_work.m_ListCtrl->GetItemText(i,4 );
		if(strcmp(str, "下载中") == 0){
			jj++;
			
		}
	}

if(jj>2)
{AfxMessageBox("最多同时只能下载3个文件!");
return 0;
}
	now=GetTickCount();
 
		CString temp1;
 
	int* dat;
dat=(int *)lParam;
CString temp;
int noth;
noth=*dat;
temp.Format("the cleek is %d",noth);
 
//	AfxMessageBox(temp);
clno=noth;

if(down[clno]==NULL){
	AfxMessageBox("no");
return 0;
}
for( i=0;i<BLOCK;i++)
::SuspendThread(m_thread[clno][i]);
 
	return 1;
 
 
}

⌨️ 快捷键说明

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