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

📄 codeprojectdlg.cpp

📁 多线程高级编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCodeProjectDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCodeProjectDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//下载按钮消息处理函数
void CCodeProjectDlg::OnOK() 
{
	m_btnOK.EnableWindow(FALSE);
	m_bWorking = TRUE;

	//取得输入的目标路径
	m_editTargetDir.GetWindowText(m_strTargetDir);
	m_strTargetDir.Replace(_T("\\"), _T("\\\\"));
	if (m_strTargetDir.Right(2) != _T("\\\\")) m_strTargetDir += _T("\\\\");

	ConfirmDir(m_strTargetDir);

	//取得到下载的项目
	m_aryDownloadItem.RemoveAll();
	m_aryDownloadItemName.RemoveAll();
	int iItemNum = m_list.GetItemCount();
	for (int i = 0; i < iItemNum; ++i)
	{
		if (ListView_GetCheckState(m_list.m_hWnd, i))
		{
			m_aryDownloadItemName.Add(m_list.GetItemText(i,0));
			m_aryDownloadItem.Add(m_list.GetItemText(i,1));
		}
	}

	m_editStatus.SetWindowText(_T(""));
	
	//新建线程处理下载任务
	::AfxBeginThread(CCodeProjectDlg::Download, this->GetSafeHwnd(), THREAD_PRIORITY_NORMAL, 0);
}

//回调函数,处理下载任务
UINT CCodeProjectDlg::Download(LPVOID pParam)
{
	HWND hWnd = (HWND)pParam;

	int i;
	CString *pStr;
	CInternetSession session("MyAgent", 1, INTERNET_OPEN_TYPE_PRECONFIG);

	pStr = new CString;
	pStr->Format(_T("%s,请等待片刻...\n"), "整个下载任务开始");
	//调用自己的消息处理函数,显示下载状态
	::PostMessage(hWnd, WM_USER_DOWNSTATUS, (WPARAM)pStr, 0);

	if (-1 == _taccess(m_strTargetDir + _T("global.css"), 0))
		if(DownloadFile(session, CString(_T("http://www.codeproject.com/styles/global.css")), m_strTargetDir + _T("global.css")) == false)
			return 0;

	//下载具体内容
	for (i = 0; i < m_aryDownloadItem.GetSize(); ++i)
	{
		pStr = new CString;
		pStr->Format(_T("%s 开始下载!\n"), m_aryDownloadItemName[i]);
		//调用自己的消息处理函数,显示下载状态
		::PostMessage(hWnd, WM_USER_DOWNSTATUS, (WPARAM)pStr, 0);
		
		CString strTemp, strCurDir;
		for(int j = 0; AfxExtractSubString(strTemp, m_aryDownloadItem[i], j, _T('/')); ++j)
			strCurDir = strTemp;
		ConfirmDir(m_strTargetDir + strCurDir + _T("\\\\"));

		//下载分类索引的HTML文件
		CString strItemGroup;
		if(DownloadHtml(session, m_aryDownloadItem[i], strItemGroup) == false)
			return 0;

		//整理HTML文件的内容
		CString strStart = _T("<!-- Main Page Contents Start -->"),
				strEnd	 = _T("<!-- Main Page Contents End -->");
		long iPotion;
		iPotion = strItemGroup.Find(strStart);
		iPotion += strStart.GetLength();
		strItemGroup.Delete(0, iPotion);
		strStart = _T("</table>");
		iPotion = strItemGroup.Find(strStart);
		iPotion += strStart.GetLength();
		strItemGroup.Delete(0, iPotion);
		iPotion = strItemGroup.Find(strEnd);
		strItemGroup.Delete(iPotion, strItemGroup.GetLength() - iPotion);
		CString strSperator = _T("</table>");
		strItemGroup.TrimRight();
		strItemGroup = strItemGroup.Left(strItemGroup.GetLength() - strSperator.GetLength());
		for (int k = -strSperator.GetLength(); -1 != k; )
		{
			iPotion = k;
			k = strItemGroup.Find(strSperator, k + strSperator.GetLength());
		}
		strItemGroup = strItemGroup.Left(iPotion);
		strEnd = _T("</table><h2>Links</h2>");
		iPotion = strItemGroup.Find(strEnd);
		if (-1 != iPotion) strItemGroup.Delete(iPotion, strItemGroup.GetLength() - iPotion);
		
		CArray<CString, CString> aryItem;
		ExtractItem(strItemGroup, CString(_T("<a href")), CString(_T(".asp")), strCurDir, &aryItem);

		//历史记录文件
		CStdioFile fileHist;
		CString strHistory;
		fileHist.Open(m_strTargetDir + strCurDir + _T("\\\\hist.txt"), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite | CFile::typeText);
		fileHist.ReadString(strHistory);
		fileHist.SeekToEnd();

		//下载文章
		int iItems = 0;
		for (; iItems < aryItem.GetSize(); ++iItems)
		{
			bool bItemComplete = true;
			CString strItemFile;
			CString strItemFileTemp;

			CString strTemp, strCurFileName;
			for(int j = 0; AfxExtractSubString(strTemp, aryItem[iItems], j, _T('/')); ++j)
				strCurFileName = strTemp;
			strCurFileName = strCurFileName.Left(strCurFileName.GetLength() - 4);
			
			int iHist = strHistory.Find(aryItem[iItems]);
			if (-1 != iHist)
			{
				strItemGroup.Replace(aryItem[iItems], strCurFileName + _T(".HTM"));
				continue;
			}
		
			if(DownloadHtml(session, _T("http://www.codeproject.com") + aryItem[iItems], strItemFile) == false)
				return 0;

			//整理文章的内容
			strStart = _T("<!-- Article Starts -->");
			strEnd   = _T("<!-- Article Ends -->");
			iPotion = strItemFile.Find(strStart, 0);
			iPotion += strStart.GetLength();
			strItemFile.Delete(0, iPotion);
			iPotion = strItemFile.Find(strEnd);
			strItemFile.Delete(iPotion, strItemFile.GetLength() - iPotion);

			strItemFileTemp = strItemFile;
			strItemFileTemp.MakeLower();

			CArray<CString, CString> aryItemSource;
			CString strFileType;
			ExtractItem(strItemFileTemp, CString(_T("<a href")), CString(_T(".zip")), strCurDir, &aryItemSource);
			ExtractItem(strItemFileTemp, CString(_T("<a href")), CString(_T(".exe")), strCurDir, &aryItemSource);
			ExtractItem(strItemFileTemp, CString(_T("<a href")), CString(_T(".rar")), strCurDir, &aryItemSource);
			ExtractItem(strItemFileTemp, CString(_T("src")), CString(_T(".jpg")), strCurDir, &aryItemSource);
			ExtractItem(strItemFileTemp, CString(_T("src")), CString(_T(".gif")), strCurDir, &aryItemSource);
			ExtractItem(strItemFileTemp, CString(_T("src")), CString(_T(".bmp")), strCurDir, &aryItemSource);
			ExtractItem(strItemFileTemp, CString(_T("src")), CString(_T(".png")), strCurDir, &aryItemSource);

			CString strCurItemDir;
			strTemp = aryItem[iItems];
			strTemp.Replace(_T("\\"), _T("/"));
			AfxExtractSubString(strCurItemDir, strTemp, (_T("/") == strTemp.Left(1)) ? 1 : 0, _T('/'));
			
			//下载文章中的资源
			for (int l = 0; l < aryItemSource.GetSize(); ++l)
			{
				CString strCurSourceFileName;
				CString strCurSourceDir = m_strTargetDir + strCurDir;
				CString strCurSourceUrl = aryItemSource[l];
				strCurSourceUrl.Replace(_T("\\"), _T("/"));
				for(int j = 0; AfxExtractSubString(strTemp, strCurSourceUrl, j, _T('/')); ++j)
				{
					strCurSourceFileName = strTemp;
					strTemp.MakeLower();
					if (-1 == strTemp.Find(_T(".")))
					{
						strCurSourceDir += _T("\\\\") + strTemp;
						ConfirmDir(strCurSourceDir);
					}
				}
				bItemComplete = bItemComplete && DownloadFile(session,
					_T("http://www.codeproject.com/") + strCurItemDir + _T("/") + strCurSourceUrl,
					strCurSourceDir + _T("\\\\") + strCurSourceFileName);

				//更新进度
				pStr = new CString;
				pStr->Format(_T("    %s 下载完毕!\n"), strCurSourceFileName);
				::PostMessage(hWnd, WM_USER_DOWNSTATUS, (WPARAM)pStr, 0);

			}//end for(l)
			
			//保存文章
			strItemFile = _T("<link rel=\"stylesheet\" type=text/css href=\"../global.css\">\n") + strItemFile;
			bItemComplete = bItemComplete && SaveStr2File(strItemFile, m_strTargetDir + strCurDir + _T("\\\\") + strCurFileName + _T(".HTM"));
			//更新进度
			pStr = new CString;
			pStr->Format(_T("  %s.htm 下载完毕!\n"), strCurFileName);
			::PostMessage(hWnd, WM_USER_DOWNSTATUS, (WPARAM)pStr, 0);
			
			fileHist.WriteString(aryItem[iItems]);
			strItemGroup.Replace(aryItem[iItems], strCurFileName + _T(".HTM"));
		}//end for(iItem)

		//保存分类索引文件		
		strItemGroup = _T("<link rel=\"stylesheet\" type=text/css href=\"../global.css\">\n") + strItemGroup;
		SaveStr2File(strItemGroup, m_strTargetDir + strCurDir + _T("\\\\index.htm"));
		//更新进度
		pStr = new CString;
		pStr->Format(_T("  index.htm 下载完毕!\n"));
		::PostMessage(hWnd, WM_USER_DOWNSTATUS, (WPARAM)pStr, 0);
		pStr = new CString;
		pStr->Format(_T("%s 下载完毕!\n"), m_aryDownloadItemName[i]);
		::PostMessage(hWnd, WM_USER_DOWNSTATUS, (WPARAM)pStr, 0);
		pStr = new CString;
		pStr->Format(_T("==================================================\n"));
		::PostMessage(hWnd, WM_USER_DOWNSTATUS, (WPARAM)pStr, 0);
		
		fileHist.Close();

	}//end for(i)
	::PostMessage(hWnd, WM_USER_DOWNCOMPLETE, 0, 0);

	return 0;
}

//自定义消息处理函数
LRESULT CCodeProjectDlg::OnDownStatus(WPARAM wParam, LPARAM lParam)
{
	CString *pStr;
	pStr = (CString *)wParam;
	
	m_editStatus.SetSel(m_editStatus.GetWindowTextLength(), m_editStatus.GetWindowTextLength());
	m_editStatus.ReplaceSel(*pStr);
	delete pStr;
	return 0;
}

//自定义消息处理函数
LRESULT CCodeProjectDlg::OnDownComplete(WPARAM, LPARAM)
{
	AfxMessageBox(_T("下载完成!"));
	m_bWorking = false;
	m_btnOK.EnableWindow();

	return 0;
}

//本程序相关说明
void CCodeProjectDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CDlgExplain dlg;
	dlg.DoModal();
}

//提取文章中内容
bool CCodeProjectDlg::ExtractItem(CString &strContant, CString &strStart, CString &strCondition, CString &strCurDir, void *pAry)
{
	CArray<CString, CString> *pArray;
	pArray = (CArray<CString, CString> *)pAry;

	CString strEnd, strNetDir = _T("http://www.codeproject.com/") + strCurDir + _T("/");
	strNetDir.MakeLower();
	int iStart = 0, iEnd = 0;
	iStart = strContant.Find(strStart, iStart);
	for (; -1 != iStart; iStart = strContant.Find(strStart, iStart + strStart.GetLength()))
	{
		bool bEnd = true;
		for(int j = 0; bEnd; ++j)
		{
			strEnd = strContant.Mid(iStart + strStart.GetLength() + j, 1);
			bEnd = (strEnd == _T(" ")) || (strEnd == _T("="));
		}
		if ((_T("\"") != strEnd) && (_T("'") != strEnd)) continue;

		iEnd = strContant.Find(strEnd, iStart + strStart.GetLength() + j);
		CString strTemp, strItem = strContant.Mid(iStart, iEnd - iStart);
		strTemp = strItem;
		strTemp.MakeLower();
		if (strTemp.Right(strCondition.GetLength()) == strCondition)
		{
			strItem = strItem.Right(strItem.GetLength() - strStart.GetLength() - j);
			strTemp = strItem.Left(strNetDir.GetLength());
			strTemp.MakeLower();
			if (_T("http://") == strTemp.Left(7))
			{
				if (strTemp.Left(strCurDir.GetLength()) == strCurDir)
				{
					strItem = strItem.Right(strItem.GetLength() - strCurDir.GetLength());
					pArray->Add(strItem);
				}
			}
			else
			{
				if (_T("ftp://") != strTemp.Left(6))
					pArray->Add(strItem);
			}
		}
	}
	return true;
}

void CCodeProjectDlg::OnStnClickedStaticWeb()
{
	// TODO: 在此添加控件通知处理程序代码
	ShellExecute(NULL, _T("open"), _T("http://www.hust.edu.cn"), NULL, NULL, 2);
}

⌨️ 快捷键说明

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