www.pudn.com > WinInet.rar > InetDownloadDlg.cpp


// InetDownloadDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "InetDownload.h" 
#include "InetDownloadDlg.h" 
#include "inetsession.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CInetDownloadDlg dialog 
 
CInetDownloadDlg::CInetDownloadDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CInetDownloadDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CInetDownloadDlg) 
	m_strURL = _T(""); 
	m_strLocalDir = _T("C:");//默认下载文件存放的目录 
	m_pThread=NULL;//还没有下载线程 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CInetDownloadDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CInetDownloadDlg) 
	DDX_Control(pDX, IDC_BUTTON2, m_buttonDownload2); 
	DDX_Control(pDX, IDC_PROGRESS1, m_progressBar); 
	DDX_Control(pDX, IDC_EDIT3, m_editStatus); 
	DDX_Control(pDX, IDC_BUTTON1, m_buttonDownload); 
	DDX_Text(pDX, IDC_EDIT1, m_strURL); 
	DDX_Text(pDX, IDC_EDIT2, m_strLocalDir); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CInetDownloadDlg, CDialog) 
	//{{AFX_MSG_MAP(CInetDownloadDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON1, OnDownload) 
	ON_BN_CLICKED(IDC_BUTTON2, OnDownload2) 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(WM_USER_THREAD_STATUS,OnThreadStatus) 
	ON_MESSAGE(WM_USER_THREAD_PROGRESS,OnThreadProgress) 
	ON_MESSAGE(WM_USER_THREAD_END,OnThreadEnd) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CInetDownloadDlg message handlers 
 
BOOL CInetDownloadDlg::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 
 
	m_progressBar.SetRange(0,100); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
// 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 CInetDownloadDlg::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 CInetDownloadDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CInetDownloadDlg::OnDownload()  
{ 
	// TODO: Add your control notification handler code here 
	if(m_pThread!=NULL) 
	{ 
		AfxMessageBox("现正下载,请等待完毕!"); 
		return; 
	} 
 
	static DLPARAM DlParam; 
	UpdateData(TRUE);//获得用户输入, 
	DlParam.strURL=m_strURL; 
	DlParam.strLocalDir=m_strLocalDir; 
	DlParam.hwnd=m_hWnd;//填充主窗口句柄 
	m_editStatus.SetWindowText("");//清除状态记录。 
	m_pThread=::AfxBeginThread(CInetDownloadDlg::DownloadURLFile,&DlParam); 
} 
 
//使用CHttpConnection类 
UINT CInetDownloadDlg::DownloadURLFile(LPVOID pParam) 
{ 
	DLPARAM* pDlParam=(DLPARAM*)pParam; 
	CString strServer,strObject; 
	DWORD nServiceType; 
	DWORD dwRet,dwRead; 
	INTERNET_PORT nPort; 
	CInetSession* pInetSession=new CInetSession(pDlParam); 
 
	CHttpConnection* pHttpConnection=NULL;	//第一步 
	CHttpFile* pHttpFile=NULL; 
	CInternetFile* pInternetFile=NULL; 
	unsigned char szBuff[1024]; 
	UINT uResult=0; 
	//分析URL,得到服务器地址、端口和目标文件。 
	if(AfxParseURL( 
		pDlParam->strURL,nServiceType,strServer,strObject,nPort)!=TRUE) 
	{ 
		delete pInetSession; 
		AfxMessageBox("URL ERROR!"); 
		return 0; 
	} 
 
	try 
	{ 
		pInetSession->EnableStatusCallback(); 
		CFile file(pDlParam->strLocalDir+strObject,CFile::modeCreate| 
			CFile::modeReadWrite|CFile::typeBinary); 
 
		if(nServiceType=AFX_INET_SERVICE_HTTP) 
		{ 
			pHttpConnection=pInetSession->GetHttpConnection(strServer,nPort);//第二步。 
			pHttpFile=pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,strObject); 
			pHttpFile->SendRequest();	//第三步 
			DWORD dwFileLen=pHttpFile->GetLength(); 
			DWORD dwProgress=0,dwPercent; 
					while((dwRead=pHttpFile->Read(szBuff,1023))>0)		//第5步 
					{ 
						file.Write(szBuff,dwRead); 
						dwProgress+=dwRead; 
						dwPercent=dwProgress*100/dwFileLen; 
						::SendMessage(pDlParam->hwnd,WM_USER_THREAD_PROGRESS, 
									dwPercent,NULL); 
					} 
			uResult=1; 
		} 
 
	} 
		catch(CInternetException* pEx) 
		{ 
			pEx->ReportError(); 
			pEx->Delete(); 
		} 
		DEL(pInternetFile); 
		DEL(pHttpFile); 
		DEL(pHttpConnection); 
		pInetSession->Close(); 
		delete pInetSession; 
		::SendMessage(pDlParam->hwnd,WM_USER_THREAD_END, 
					uResult,NULL); 
		return uResult; 
} 
 
LRESULT CInetDownloadDlg::OnThreadEnd(WPARAM wParam, LPARAM lParam) 
{ 
	//下载结束。 
	UINT uResult=(UINT)wParam; 
	if(uResult==1) 
		AfxMessageBox("下载结束,操作成功!"); 
	else 
		AfxMessageBox("下载失败,请查明原因!"); 
	m_pThread=NULL; 
	return 1; 
} 
 
LRESULT CInetDownloadDlg::OnThreadProgress(WPARAM wParam, LPARAM lParam) 
{ 
	m_progressBar.SetPos(wParam); 
	return 1; 
} 
 
LRESULT CInetDownloadDlg::OnThreadStatus(WPARAM wParam, LPARAM lParam) 
{ 
	LPCSTR lpcstrStatus=(LPCSTR)lParam; 
	int nLen; 
	nLen=m_editStatus.GetWindowTextLength(); 
	m_editStatus.SetSel(nLen,nLen); 
	m_editStatus.ReplaceSel(lpcstrStatus); 
	return 1; 
} 
 
 
//使用OpenURL. 
UINT CInetDownloadDlg::DownloadURLFile2(LPVOID pParam) 
{ 
	DLPARAM* pDlParam=(DLPARAM*)pParam; 
	CString strServer,strObject; 
	DWORD nServiceType; 
	DWORD dwRet,dwRead; 
	INTERNET_PORT nPort; 
	CInetSession* pInetSession=new CInetSession(pDlParam); 
	CStdioFile* pStudioFile=NULL; 
 
	unsigned char szBuff[1024]; 
	UINT uResult=0; 
	//分析URL,得到服务器地址、端口和目标文件。 
	if(AfxParseURL( 
		pDlParam->strURL,nServiceType,strServer,strObject,nPort)!=TRUE) 
	{ 
		delete pInetSession; 
		AfxMessageBox("URL ERROR!"); 
		return 0; 
	} 
 
	try 
	{ 
		pInetSession->EnableStatusCallback(); 
		CFile file(pDlParam->strLocalDir+strObject,CFile::modeCreate| 
			CFile::modeReadWrite|CFile::typeBinary); 
 
		if(nServiceType=AFX_INET_SERVICE_HTTP) 
		{			 
			pStudioFile=pInetSession->OpenURL(pDlParam->strURL,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_KEEP_CONNECTION); 
			DWORD dwFileLen=pStudioFile->GetLength(); 
 
			DWORD dwProgress=0,dwPercent; 
					while((dwRead=pStudioFile->Read(szBuff,1023))>0) 
					{ 
						file.Write(szBuff,dwRead); 
						dwProgress+=dwRead; 
						dwPercent=dwProgress*100/dwFileLen; 
						::SendMessage(pDlParam->hwnd,WM_USER_THREAD_PROGRESS, 
									dwPercent,NULL); 
					} 
			uResult=1; 
		} 
 
	} 
		catch(CInternetException* pEx) 
		{ 
			pEx->ReportError(); 
			pEx->Delete(); 
		} 
		pInetSession->Close(); 
		delete pInetSession; 
		::SendMessage(pDlParam->hwnd,WM_USER_THREAD_END, 
					uResult,NULL); 
		return uResult; 
} 
 
 
 
void CInetDownloadDlg::OnDownload2()  
{ 
	// TODO: Add your control notification handler code here 
	if(m_pThread!=NULL) 
	{ 
		AfxMessageBox("现正下载,请等待完毕!"); 
		return; 
	} 
 
	static DLPARAM DlParam; 
	UpdateData(TRUE);//获得用户输入, 
	DlParam.strURL=m_strURL; 
	DlParam.strLocalDir=m_strLocalDir; 
	DlParam.hwnd=m_hWnd;//填充主窗口句柄 
	m_editStatus.SetWindowText("");//清除状态记录。 
	m_pThread=::AfxBeginThread(CInetDownloadDlg::DownloadURLFile2,&DlParam); 
}