www.pudn.com > NetPaw.rar > DownloadDlg.cpp


// DownloadDlg.cpp : 实现文件 
// 
 
#include "stdafx.h" 
#include "NetPaw.h" 
#include "mainfrm.h" 
#include "netpawdoc.h" 
#include "downloadfile.h" 
#include ".\downloaddlg.h" 
 
 
// CDownloadDlg 对话框 
// http://www.6663.net/soft/X-26.rar 
 
IMPLEMENT_DYNAMIC(CDownloadDlg, CDialog) 
CDownloadDlg::CDownloadDlg(CWnd* pParent) 
	: CDialog(CDownloadDlg::IDD, pParent) 
	, m_sDownloadUrl(_T("")) 
	, m_sReferer(_T("")) 
	, m_sWebInfo(_T("")) 
	, m_sSavePath(_T("")) 
	, m_nConnections(0) 
{ 
} 
 
CDownloadDlg::~CDownloadDlg() 
{ 
} 
 
void CDownloadDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	DDX_Text(pDX, IDC_EDTURL, m_sDownloadUrl); 
	DDV_MaxChars(pDX, m_sDownloadUrl, MAX_PATH-1); 
	DDX_Text(pDX, IDC_EDTPATH, m_sSavePath); 
	DDV_MaxChars(pDX, m_sSavePath, MAX_PATH-1); 
	DDX_Text(pDX, IDC_EDTREFER, m_sReferer); 
	DDV_MaxChars(pDX, m_sReferer, MAX_PATH-1); 
	DDX_Text(pDX, IDC_EDTCONNS, m_nConnections); 
	DDV_MinMaxLong(pDX, m_nConnections, 1, MAX_CONNECTIONS); 
} 
 
 
BEGIN_MESSAGE_MAP(CDownloadDlg, CDialog) 
	ON_BN_CLICKED(IDOK, OnBnClickedDownload) 
	ON_BN_CLICKED(IDC_BTNFOLDER, OnBnClickedFolder) 
END_MESSAGE_MAP() 
 
 
// CDownloadDlg 消息处理程序 
 
void CDownloadDlg::OnBnClickedDownload() 
{ 
	// 从控件更新数据 
	UpdateData(TRUE); 
 
	if( m_sDownloadUrl.IsEmpty() || m_sSavePath.IsEmpty() ) 
	{ 
		return; 
	} 
 
	// get local pathname of the file 
	CFileFind finder; 
	BOOL bContinue = finder.FindFile( m_sSavePath ); 
	finder.Close(); 
	if( !bContinue ) 
	{ 
		if( !CreateDirectory(m_sSavePath, NULL) ) 
		{ 
			OnOK(); 
			AfxMessageBox("创建保存文件夹错误\n"); 
			return; 
		} 
	} 
 
	// save global data 
	CNetPawApp *pApp = (CNetPawApp *)AfxGetApp(); 
	if( pApp ) 
	{ 
		pApp->m_sSavePath = m_sSavePath; 
		pApp->m_nConnections = m_nConnections; 
	} 
 
	// allocate download file objects 
	CNetPawDoc *pDoc = GetDocument(); 
	if( pDoc ) 
	{ 
		DLFILEITEM_S stDlFile; 
		ZeroMemory( &stDlFile, sizeof(DLFILEITEM_S) ); 
		stDlFile.wBlocksToDo = (WORD)m_nConnections; 
		StrCopy( stDlFile.szUrl, m_sDownloadUrl, MAX_PATH ); 
		StrCopy( stDlFile.szReferer, m_sReferer, MAX_PATH ); 
		StrCopy( stDlFile.szWebInfo, m_sWebInfo, MAX_PATH/2 ); 
 
		// new download file object and start 
		CDownloadFile *pDlFile = pDoc->NewDownldFile( &stDlFile ); 
		pDlFile->Start( AfxGetMainWnd()->GetSafeHwnd() ); 
	} 
 
	OnOK(); 
} 
 
CNetPawDoc* CDownloadDlg::GetDocument(void) 
{ 
	return ( (AfxGetMainWnd() != NULL) ? 
		(CNetPawDoc *)(((CMainFrame *)AfxGetMainWnd())->GetActiveDocument()) : NULL ); 
} 
 
void CDownloadDlg::OnBnClickedFolder() 
{ 
	BROWSEINFO brsInfo; 
	::ZeroMemory( &brsInfo, sizeof(BROWSEINFO) ); 
 
	// fill info structure 
	brsInfo.hwndOwner = this->GetSafeHwnd(); 
	brsInfo.lpszTitle = _T("选择下载文件保存路径"); 
 
	// if choose ok, pItList != NULL 
	ITEMIDLIST *pItList = ::SHBrowseForFolder(&brsInfo); 
	if( pItList != NULL ) 
	{ 
		char szFullName[MAX_PATH]; 
		if( ::SHGetPathFromIDList(pItList, szFullName) ) 
		{ 
			m_sSavePath = szFullName; 
 
			// after choosing path, show the path 
			UpdateData(FALSE); 
		} 
 
        // free memory used 
        IMalloc *pIMalloc = NULL; 
        if( SUCCEEDED( SHGetMalloc( &pIMalloc ) ) ) 
        { 
            pIMalloc->Free ( pItList ); 
            pIMalloc->Release(); 
        } 
	}	 
} 
 
 
BOOL CDownloadDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// TODO:  在此添加额外的初始化 
	// Set spin control 
	CSpinButtonCtrl *pSpin = (CSpinButtonCtrl *)GetDlgItem(IDC_SPIN1); 
	if( pSpin == NULL ) 
	{ 
		return FALSE; 
	} 
 
	UDACCEL stAccel; 
	stAccel.nInc = 1; 
	stAccel.nSec = 1; 
	pSpin->SetRange(1, MAX_CONNECTIONS); 
	pSpin->SetAccel(1, &stAccel); 
 
	return TRUE;  // return TRUE unless you set the focus to a control 
	// 异常: OCX 属性页应返回 FALSE 
}