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

📄 codeprojectdlg.cpp

📁 多线程高级编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CodeProjectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CodeProject.h"
#include "CodeProjectDlg.h"
#include "DlgExplain.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//以下为用户自定义消息
#define WM_USER_DOWNSTATUS		(WM_USER + 101)
#define WM_USER_DOWNCOMPLETE	(WM_USER + 102)

/////////////////////////////////////////////////////////////////////////////
// CCodeProjectDlg dialog

//根据指定的URL,下载下载指定的网页到一个字符串中
bool DownloadHtml(CInternetSession &session, CString &strURL, CString &outStr)
{
	CString sHeader = _T("Accept:*/*\r\n");
	//设置下载选项标识
	DWORD dwFlags = INTERNET_FLAG_DONT_CACHE |
					INTERNET_FLAG_TRANSFER_BINARY |
					INTERNET_FLAG_EXISTING_CONNECT |
					INTERNET_FLAG_RELOAD;
	int nRead;
	char buffer[1024];
	
	try
	{
		if(strURL.Left(7) != _T("http://")) strURL = _T("http://") + strURL;

		//根据指定的URL,建立http传输会话
		CHttpFile *pHttpFile = (CHttpFile*)session.OpenURL(strURL, 1, dwFlags, sHeader, -1L);
		if(pHttpFile)
		{
			//循环读取数据到缓冲区
			do
			{
				nRead = pHttpFile->Read(buffer, 1023);
				if(nRead != 0)
				{
					buffer[nRead] = 0;
					outStr += buffer;
				}
			} while (nRead != 0);
			delete pHttpFile;

		}
		else
		{
			AfxMessageBox(_T("建立连接失败:") + strURL);
			return false;
		}
	}
	catch (CInternetException *pEx)
	{
		AfxMessageBox(_T("访问网址时出现异常:") + strURL);
		pEx->Delete();
		return false;
	}
	return true;
}

//确定用户指定的目录存在与否,如不存在则新建
bool ConfirmDir(CString &strDir)
{
	if (-1 == _taccess(strDir, 0))
		if (!::CreateDirectory(strDir, NULL))
		{
			AfxMessageBox(_T("创建文件夹失败!") + strDir);
			return false;
		}
	return true;
}

//下载指定的文件
bool DownloadFile(CInternetSession &session, CString &strURL, CString &strFileName)
{
	CString sHeader = _T("Accept:*/*\r\n");
	//设置下载选项标识
	DWORD dwFlags = INTERNET_FLAG_DONT_CACHE |
					INTERNET_FLAG_TRANSFER_BINARY |
					INTERNET_FLAG_EXISTING_CONNECT |
					INTERNET_FLAG_RELOAD;
	int nRead;
	char buffer[1024]; 
	CHttpFile *pHttpFile = NULL;
	CFile *pFile = NULL;

	try
	{
		if(strURL.Left(7) != _T("http://")) strURL = _T("http://") + strURL;
		//建立会话,并打开指定URL
		pHttpFile = (CHttpFile*)session.OpenURL(strURL, 1, dwFlags, sHeader, -1L);
		if(pHttpFile)
		{
			pFile = new CFile;
			pFile->Open(strFileName, CFile::modeWrite | CFile::modeCreate);
			do
			{
				nRead = pHttpFile->Read(buffer, 1023);
				if(nRead != 0)
				{
					buffer[nRead] = 0;
					pFile->Write(buffer, nRead);
				}
			} while (nRead != 0);
			delete pHttpFile;
			pFile->Close();
			delete pFile;
		}
		else
		{
			AfxMessageBox(_T("建立连接失败:") + strURL);
			return false;
		}
	}
	catch (CInternetException *pEx)
	{
		AfxMessageBox(_T("访问网址时出现异常:") + strURL);
		pEx->Delete();
		if (NULL != pHttpFile) delete pHttpFile;
		if (NULL != pFile) delete pFile;
		return false;
	}
	catch (CFileException * pFEx)
	{
		AfxMessageBox(_T("写文件时出现异常!") + strURL + _T("->") + strFileName);
		pFEx->Delete();
		if (NULL != pHttpFile) delete pHttpFile;
		if (NULL != pFile) delete pFile;
		return false;
	}
	return true;
}

//将字符串中的内容保存为文件
bool SaveStr2File(CString &inStr, CString &strFileName)
{
	CFile *pFile = new CFile;
	pFile->Open(strFileName, CFile::modeWrite | CFile::modeCreate);
	inStr.FreeExtra();
	long lStrLen = inStr.GetLength() * sizeof(TCHAR);
	pFile->Write((void*)inStr.GetBuffer(lStrLen), lStrLen);
	pFile->Close();
	delete pFile;
	return true;
}

CString CCodeProjectDlg::m_strTargetDir;//目标文件夹
CArray<CString, CString> CCodeProjectDlg::m_aryDownloadItem;//待下载项
CArray<CString, CString> CCodeProjectDlg::m_aryDownloadItemName;//待下载项名

CCodeProjectDlg::CCodeProjectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCodeProjectDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCodeProjectDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCodeProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCodeProjectDlg)
	DDX_Control(pDX, IDOK, m_btnOK);
	DDX_Control(pDX, IDC_EDIT1, m_editStatus);
	DDX_Control(pDX, IDC_LIST, m_list);
	DDX_Control(pDX, IDC_EDIT_TargetDir, m_editTargetDir);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCodeProjectDlg, CDialog)
	//{{AFX_MSG_MAP(CCodeProjectDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//用户自定义消息关联
	ON_MESSAGE(WM_USER_DOWNSTATUS, OnDownStatus)
	ON_MESSAGE(WM_USER_DOWNCOMPLETE, OnDownComplete)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
	ON_STN_CLICKED(IDC_STATIC_WEB, OnStnClickedStaticWeb)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCodeProjectDlg message handlers

BOOL CCodeProjectDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	CString strItem[] = {
		_T(".NET -> Managed C++"),
		_T(".NET -> SOAP and XML"),
		_T(".NET -> C++ Web Services"),
		_T("MFC Controls -> Button Controls"),
		_T("MFC Controls -> Dialog & Windows"),
		_T("MFC Controls -> Combo & List Boxes"),
		_T("MFC Controls -> Menus"),
		_T("MFC Controls -> Toolbars & Docking Windows"),
		_T("MFC Controls -> Status Bar"),
		_T("MFC Controls -> Edit Controls"),
		_T("MFC Controls -> List Controls"),
		_T("MFC Controls -> Tree Controls"),
		_T("MFC Controls -> Tab Controls"),
		_T("MFC Controls -> Property Sheets"),
		_T("MFC Controls -> Rich Edit Controls"),
		_T("MFC Controls -> Splitter Windows"),
		_T("MFC Controls -> Static Controls"),
		_T("MFC Controls -> Miscellaneous Controls"),
		_T("General - > COM/DCOM/COM+"),
		_T("General - > C++/MFC"),
		_T("General - > Clipboard"),
		_T("General - > Doc/View"),
		_T("General - > Debug tips"),
		_T("General - > Database"),
		_T("General - > Dlls"),
		_T("General - > Files & Folders"),
		_T("General - > Internet & Network"),
		_T("General - > Macros and Add-ins"),
		_T("General - > Printing"),
		_T("General - > Samples"),
		_T("General - > String"),
		_T("General - > System"),
		_T("General - > Threads, Processes & IPC"),
		_T("General - > Programming tips"),
		_T("General - > Free Tools"),
		_T("General - > Shell Programming"),
		_T("General - > Date & Time"),
		_T("General - > WinHelp/HTMLHelp"),
		_T("Libraries - > ATL"),
		_T("Libraries - > WTL"),
		_T("Libraries - > STL"),
		_T("Libraries - > Libraries & Projects"),
		_T("MultiMedia - > Bitmaps & Palettes"),
		_T("MultiMedia - > Audio & Video"),
		_T("MultiMedia - > DirectX"),
		_T("MultiMedia - > OpenGL"),
		_T("MultiMedia - > Fonts & GDI"),
		_T("MultiMedia - > GDI+"),
		_T("Platforms - > Win32/SDK"),
		_T("Platforms - > Windows 2000/XP"),
		_T("Platforms - > CE/Embedded"),
		_T("Web/Scripting - > ISAPI")
	};
	CString strItemUrl[] = {
		_T("http://www.codeproject.com/managedcpp"),
		_T("http://www.codeproject.com/soap"),
		_T("http://www.codeproject.com/webservices"),
		_T("http://www.codeproject.com/buttonctrl"),
		_T("http://www.codeproject.com/dialog"),
		_T("http://www.codeproject.com/combobox"),
		_T("http://www.codeproject.com/menu"),
		_T("http://www.codeproject.com/docking"),
		_T("http://www.codeproject.com/statusbar"),
		_T("http://www.codeproject.com/editctrl"),
		_T("http://www.codeproject.com/listctrl"),
		_T("http://www.codeproject.com/treectrl"),
		_T("http://www.codeproject.com/tabctrl"),
		_T("http://www.codeproject.com/property"),
		_T("http://www.codeproject.com/richedit"),
		_T("http://www.codeproject.com/splitter"),
		_T("http://www.codeproject.com/staticctrl"),
		_T("http://www.codeproject.com/miscctrl"),
		_T("http://www.codeproject.com/com"),
		_T("http://www.codeproject.com/cpp"),
		_T("http://www.codeproject.com/clipboard"),
		_T("http://www.codeproject.com/docview"),
		_T("http://www.codeproject.com/debug"),
		_T("http://www.codeproject.com/database"),
		_T("http://www.codeproject.com/dll"),
		_T("http://www.codeproject.com/file"),
		_T("http://www.codeproject.com/internet"),
		_T("http://www.codeproject.com/macro"),
		_T("http://www.codeproject.com/printing"),
		_T("http://www.codeproject.com/samples"),
		_T("http://www.codeproject.com/string"),
		_T("http://www.codeproject.com/system"),
		_T("http://www.codeproject.com/threads"),
		_T("http://www.codeproject.com/tips"),
		_T("http://www.codeproject.com/tools"),
		_T("http://www.codeproject.com/shell"),
		_T("http://www.codeproject.com/datetime"),
		_T("http://www.codeproject.com/winhelp"),
		_T("http://www.codeproject.com/atl"),
		_T("http://www.codeproject.com/wtl"),
		_T("http://www.codeproject.com/vcpp/stl"),
		_T("http://www.codeproject.com/library"),
		_T("http://www.codeproject.com/bitmap"),
		_T("http://www.codeproject.com/audio"),
		_T("http://www.codeproject.com/directx"),
		_T("http://www.codeproject.com/opengl"),
		_T("http://www.codeproject.com/gdi"),
		_T("http://www.codeproject.com/gdiplus"),
		_T("http://www.codeproject.com/win32"),
		_T("http://www.codeproject.com/w2k"),
		_T("http://www.codeproject.com/ce"),
		_T("http://www.codeproject.com/isapi")
	};
	
	//初始化列视图
	ListView_SetExtendedListViewStyle(m_list.m_hWnd, LVS_EX_FULLROWSELECT|LVS_EX_INFOTIP|LVS_EX_FLATSB|LVS_EX_GRIDLINES|LVS_EX_CHECKBOXES);
	m_list.InsertColumn(0, _T("下载项目"), LVCFMT_CENTER, 300);
	m_list.InsertColumn(1, _T("URL"), LVCFMT_LEFT, 300);

	//显示可选的下载项目
	for(int i = 0; i < sizeof(strItem) / sizeof(CString); ++i)
	{
		m_list.InsertItem(i, strItem[i]);
		m_list.SetItemText(i, 1, strItemUrl[i]);
	}
	
	//默认下载目的文件夹
	m_editTargetDir.SetWindowText(_T("D:\\MyTargetFolder"));

	m_bWorking = FALSE;
	//m_btnOK.EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

⌨️ 快捷键说明

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